Skip to content

Instantly share code, notes, and snippets.

@DimitrK
Last active October 4, 2017 10:06
Show Gist options
  • Select an option

  • Save DimitrK/bb991c16c249e1a0e1b0985412e07e22 to your computer and use it in GitHub Desktop.

Select an option

Save DimitrK/bb991c16c249e1a0e1b0985412e07e22 to your computer and use it in GitHub Desktop.
Validate parameters or variables with a more declarative way.
/**
* Copyright 2017 dimitrk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Takes an input and validates it
* @param {*} input - The input to validate
*/
var expect = function(input) {
return {
/**
* Equality check among the expect value and the passed value on the function
* @param {*} - A value to be equal with
* @param {string} - A custom error message
* @returns {boolean} - `true` if valid
* @throws {Error} - when expect value is not equal with the one passed.
*/
toEqual: function(val, errorMsg) {
if (val !== input) {
throw new Error(errorMsg || input + ' expected to be equal to ' + val);
} else {
return true;
}
},
/**
* Check if the expected value is of the same type as the given parameter in toBe
* @param {function|array} type - The type as a constructor function or as an array
* of one or more functions. If more than one constructor functions exist then it
* will not throw if at least one is valid.
* @param {string} errorMsg - The custom error message.
* @returns {boolean} - `true` if valid
* @throws {TypeError} - when expect value has no valid type of the ones passed.
*/
toBe: function(type, errorMsg) {
if (type instanceof Array) {
var oneOfIsValid;
oneOfIsValid = type.reduce(function(memo, cmp) {
try {
return memo === undefined && expect(input).toBe(cmp) || memo;
} catch (e) {
return memo;
}
}, oneOfIsValid);
if (!oneOfIsValid) {
throw new TypeError(errorMsg || 'Expected input to be one of the types');
} else {
return true;
}
}
var expected = '';
//Handling special cases
switch (type) {
case Number:
// Avoid NaN as number check
expected = isNaN(input) ? 'Foo': 'Number';
break;
case null:
// null has not prototype
expected = "Null";
break;
case undefined:
// undefined has not prototype
expected = "Undefined";
break;
default:
expected = type.prototype.constructor.name;
break;
}
if ( (Object.prototype.toString.call(input) === '[object ' + expected + ']') || //Undefined/null would throw on getPrototypeOf.
input.constructor === type || // IE will break at getPrototypeOf for primitives
Object.getPrototypeOf(input) === type.prototype ){
return true;
}
throw new TypeError(errorMsg || 'Expected input to be ' + expected);
}
};
};
function filterAdults(peopleArray) {
expect(peopleArray).toBe(Array);
peopleArray.filter((p) => {
return p.age >= 18;
})
}
filterAdults([
{
name: 'dimitrk',
age: 20,
},
{
name: 'ivan',
age: 12,
},
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment