Skip to content

Commit

Permalink
refactor: extract some code
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 30, 2019
1 parent afe27c0 commit 1cff857
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 71 deletions.
72 changes: 1 addition & 71 deletions src/license-plugin-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,77 +26,7 @@

const _ = require('lodash');
const PLUGIN_NAME = require('./license-plugin-name.js');

const validators = {
string() {
return {
type: 'object.type.string',
message: 'must be a string',
schema: null,
test(value) {
return _.isString(value);
},
};
},

boolean() {
return {
type: 'object.type.boolean',
message: 'must be a boolean',
schema: null,
test(value) {
return _.isBoolean(value);
},
};
},

func() {
return {
type: 'object.type.func',
message: 'must be a function',
schema: null,
test(value) {
return _.isFunction(value);
},
};
},

object(schema) {
return {
type: 'object.type.object',
message: 'must be an object',
schema,
test(value) {
return _.isObject(value) &&
!_.isArray(value) &&
!_.isFunction(value) &&
!_.isNil(value) &&
!_.isString(value) &&
!_.isNumber(value);
},
};
},

array(schema) {
return {
type: 'object.type.array',
message: 'must be an array',
schema,
test(value) {
return _.isArray(value);
},
};
},

any() {
return {
type: 'object.any',
message: null,
schema: null,
test: () => true,
};
},
};
const validators = require('./schema-validators.js');

/**
* Format given array of path to a human readable path.
Expand Down
153 changes: 153 additions & 0 deletions src/schema-validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2018 Mickael Jeanroy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict';

const _ = require('lodash');

/**
* Check if given value is a `string`.
*
* @param {*} value The value to check.
* @return {boolean} `true` if `value` is a string, `false` otherwise.
*/
function isString(value) {
return _.isString(value);
}

/**
* Check if given value is a `boolean`.
*
* @param {*} value The value to check.
* @return {boolean} `true` if `value` is a boolean, `false` otherwise.
*/
function isBoolean(value) {
return _.isBoolean(value);
}

/**
* Check if given value is a `function`.
*
* @param {*} value The value to check.
* @return {boolean} `true` if `value` is a function, `false` otherwise.
*/
function isFunction(value) {
return _.isFunction(value);
}

/**
* Check if given value is a `number`.
*
* @param {*} value The value to check.
* @return {boolean} `true` if `value` is a number, `false` otherwise.
*/
function isNumber(value) {
return _.isNumber(value);
}

/**
* Check if given value is `null` or `undefined`.
*
* @param {*} value The value to check.
* @return {boolean} `true` if `value` is `null` or `undefined`, `false` otherwise.
*/
function isNil(value) {
return _.isNil(value);
}

/**
* Check if given value is an `array`.
*
* @param {*} value The value to check.
* @return {boolean} `true` if `value` is an array, `false` otherwise.
*/
function isArray(value) {
return _.isArray(value);
}

/**
* Check if given value is an plain object.
*
* @param {*} value The value to check.
* @return {boolean} `true` if `value` is a plain object, `false` otherwise.
*/
function isObject(value) {
return _.isObject(value) && !isArray(value) && !isFunction(value) && !isNil(value) && !isString(value) && !isNumber(value);
}

module.exports = {
string() {
return {
type: 'object.type.string',
message: 'must be a string',
schema: null,
test: isString,
};
},

boolean() {
return {
type: 'object.type.boolean',
message: 'must be a boolean',
schema: null,
test: isBoolean,
};
},

func() {
return {
type: 'object.type.func',
message: 'must be a function',
schema: null,
test: isFunction,
};
},

object(schema) {
return {
type: 'object.type.object',
message: 'must be an object',
schema,
test: isObject,
};
},

array(schema) {
return {
type: 'object.type.array',
message: 'must be an array',
schema,
test: isArray,
};
},

any() {
return {
type: 'object.any',
message: null,
schema: null,
test: () => true,
};
},
};
107 changes: 107 additions & 0 deletions test/schema-validators.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2018 Mickael Jeanroy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const validators = require('../dist/schema-validators.js');

describe('schema validators', () => {
it('should validate string', () => {
const stringValidator = validators.string();

expect(stringValidator.type).toBe('object.type.string');
expect(stringValidator.message).toBe('must be a string');

expect(stringValidator.test('')).toBe(true);
expect(stringValidator.test(String(''))).toBe(true);
expect(stringValidator.test(null)).toBe(false);
expect(stringValidator.test(undefined)).toBe(false);
expect(stringValidator.test(false)).toBe(false);
});

it('should validate boolean', () => {
const booleanValidator = validators.boolean();

expect(booleanValidator.type).toBe('object.type.boolean');
expect(booleanValidator.message).toBe('must be a boolean');

expect(booleanValidator.test(true)).toBe(true);
expect(booleanValidator.test(false)).toBe(true);
expect(booleanValidator.test(null)).toBe(false);
expect(booleanValidator.test(undefined)).toBe(false);
expect(booleanValidator.test('false')).toBe(false);
expect(booleanValidator.test('true')).toBe(false);
});

it('should validate function', () => {
const funcValidator = validators.func();

expect(funcValidator.type).toBe('object.type.func');
expect(funcValidator.message).toBe('must be a function');

expect(funcValidator.test(() => {})).toBe(true);
expect(funcValidator.test(function test() {})).toBe(true);
expect(funcValidator.test(null)).toBe(false);
expect(funcValidator.test(undefined)).toBe(false);
expect(funcValidator.test('')).toBe(false);
});

it('should validate array', () => {
const arrayValidator = validators.array();

expect(arrayValidator.type).toBe('object.type.array');
expect(arrayValidator.message).toBe('must be an array');

expect(arrayValidator.test([])).toBe(true);
expect(arrayValidator.test(null)).toBe(false);
expect(arrayValidator.test(undefined)).toBe(false);
expect(arrayValidator.test('')).toBe(false);
expect(arrayValidator.test({})).toBe(false);
});

it('should validate object', () => {
const objectValidator = validators.object();

expect(objectValidator.type).toBe('object.type.object');
expect(objectValidator.message).toBe('must be an object');

expect(objectValidator.test({})).toBe(true);
expect(objectValidator.test(null)).toBe(false);
expect(objectValidator.test(undefined)).toBe(false);
expect(objectValidator.test('')).toBe(false);
expect(objectValidator.test([])).toBe(false);
expect(objectValidator.test(1)).toBe(false);
});

it('should validate any', () => {
const anyValidator = validators.any();

expect(anyValidator.type).toBe('object.any');
expect(anyValidator.message).toBeNull();

expect(anyValidator.test([])).toBe(true);
expect(anyValidator.test(null)).toBe(true);
expect(anyValidator.test(undefined)).toBe(true);
expect(anyValidator.test('')).toBe(true);
expect(anyValidator.test({})).toBe(true);
});
});

0 comments on commit 1cff857

Please sign in to comment.