pax_global_header 0000666 0000000 0000000 00000000064 13134463307 0014516 g ustar 00root root 0000000 0000000 52 comment=44e5b5808a46a6e22fee66ad2508d2d1a0e1477c
value-or-function-3.0.0/ 0000775 0000000 0000000 00000000000 13134463307 0015073 5 ustar 00root root 0000000 0000000 value-or-function-3.0.0/.editorconfig 0000664 0000000 0000000 00000000305 13134463307 0017546 0 ustar 00root root 0000000 0000000 # http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
value-or-function-3.0.0/.eslintrc 0000664 0000000 0000000 00000000030 13134463307 0016710 0 ustar 00root root 0000000 0000000 {
"extends": "gulp"
}
value-or-function-3.0.0/.gitignore 0000664 0000000 0000000 00000001016 13134463307 0017061 0 ustar 00root root 0000000 0000000 # Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
value-or-function-3.0.0/.jscsrc 0000664 0000000 0000000 00000000027 13134463307 0016362 0 ustar 00root root 0000000 0000000 {
"preset": "gulp"
}
value-or-function-3.0.0/.travis.yml 0000664 0000000 0000000 00000000171 13134463307 0017203 0 ustar 00root root 0000000 0000000 sudo: false
language: node_js
node_js:
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
after_script:
- npm run coveralls
value-or-function-3.0.0/LICENSE 0000664 0000000 0000000 00000002142 13134463307 0016077 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2015 Blaine Bublitz, Eric Schoffstall and other contributors
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.
value-or-function-3.0.0/README.md 0000664 0000000 0000000 00000010411 13134463307 0016347 0 ustar 00root root 0000000 0000000
# value-or-function
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url]
Normalize a value or function, applying extra args to the function
## Example
```js
var normalize = require('value-or-function');
// Values matching type are returned
var isEnabled = normalize('boolean', true);
// isEnabled === true
// Values not matching type return undefined
var isEnabled = normalize('boolean', 1);
// isEnabled === undefined
// Functions are called
var isEnabled = normalize('boolean', function() {
return false;
});
// isEnabled === false
// Extra arguments are applied to function
var count = normalize('number', function(a, b) {
return a + b;
}, 1, 2);
// count === 3
// Supply the function with context
var context = { c: 3 };
var count = normalize.call(context, 'number', function(a, b) {
return a + b + this.c;
}, 1, 2);
// count === 6
// Values one of multiple types are returned
var isEnabled = normalize(['string', 'boolean'], true);
// isEnabled === true
// Provide a function as first argument to do custom coercion
var now = new Date();
var enabledSince = normalize(function(value) {
if (value.constructor === Date) {
return value;
}
}, now);
// enabledSince === now
// Convenience methods are available for the built-in types
var result = normalize.object({});
var result = normalize.number(1);
var result = normalize.string('');
var result = normalize.symbol(Symbol());
var result = normalize.boolean(true);
var result = normalize.function(function() {});
var result = normalize.date(new Date());
```
## API
### `normalize(coercer, value[, ...appliedArguments])`
Takes a coercer function `coercer` to transform `value` to the desired type.
Also optionally takes any extra arguments to apply to `value` if `value` is a function.
If the return value of `coercer(value)` is not `null` or `undefined`, that value is returned.
Otherwise, if `value` is a function, that function is called with any extra arguments
supplied to `normalize`, and its return value is passed through the coercer.
If `coercer` is a string, it must be one of the built-in types (see below)
and the appropriate default coercer is invoked, optionally first reducing `value`
to a primitive type with `.valueOf()` if it is an Object.
If `coercer` is an array, each element is tried until one returns something other
than `null` or `undefined`, or it results in `undefined` if all of the elements yield `null` or `undefined`.
#### `normalize.object(value[, ...appliedArguments])`
Convenience method for `normalize('object', ...)`.
#### `normalize.number(value[, ...appliedArguments])`
Convenience method for `normalize('number', ...)`.
#### `normalize.string(value[, ...appliedArguments])`
Convenience method for `normalize('string', ...)`.
#### `normalize.symbol(value[, ...appliedArguments])`
Convenience method for `normalize('symbol', ...)`.
#### `normalize.boolean(value[, ...appliedArguments])`
Convenience method for `normalize('boolean', ...)`.
#### `normalize.function(value[, ...appliedArguments])`
Convenience method for `normalize('function', ...)`.
#### `normalize.date(value[, ...appliedArguments])`
Convenience method for `normalize('date', ...)`.
## License
MIT
[downloads-image]: http://img.shields.io/npm/dm/value-or-function.svg
[npm-url]: https://npmjs.org/package/value-or-function
[npm-image]: http://img.shields.io/npm/v/value-or-function.svg
[travis-url]: https://travis-ci.org/gulpjs/value-or-function
[travis-image]: http://img.shields.io/travis/gulpjs/value-or-function.svg?label=travis-ci
[appveyor-url]: https://ci.appveyor.com/project/gulpjs/value-or-function
[appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/value-or-function.svg?label=appveyor
[coveralls-url]: https://coveralls.io/r/gulpjs/value-or-function
[coveralls-image]: http://img.shields.io/coveralls/gulpjs/value-or-function/master.svg
[gitter-url]: https://gitter.im/gulpjs/gulp
[gitter-image]: https://badges.gitter.im/gulpjs/gulp.png
value-or-function-3.0.0/appveyor.yml 0000664 0000000 0000000 00000000721 13134463307 0017463 0 ustar 00root root 0000000 0000000 # http://www.appveyor.com/docs/appveyor-yml
# http://www.appveyor.com/docs/lang/nodejs-iojs
environment:
matrix:
# node.js
- nodejs_version: "0.10"
- nodejs_version: "0.12"
- nodejs_version: "4"
- nodejs_version: "5"
- nodejs_version: "6"
install:
- ps: Install-Product node $env:nodejs_version
- npm install
test_script:
- node --version
- npm --version
- cmd: npm test
build: off
# build version format
version: "{build}"
value-or-function-3.0.0/index.js 0000664 0000000 0000000 00000004131 13134463307 0016537 0 ustar 00root root 0000000 0000000 'use strict';
// Built-in types
var types = [
'object',
'number',
'string',
'symbol',
'boolean',
'date',
'function', // Weird to expose this
];
function normalize(coercer, value) {
if (typeof value === 'function') {
if (coercer === 'function') {
return value;
}
value = value.apply(this, slice(arguments, 2));
}
return coerce(this, coercer, value);
}
function coerce(ctx, coercer, value) {
// Handle built-in types
if (typeof coercer === 'string') {
if (coerce[coercer]) {
return coerce[coercer].call(ctx, value);
}
return typeOf(coercer, value);
}
// Handle custom coercer
if (typeof coercer === 'function') {
return coercer.call(ctx, value);
}
// Array of coercers, try in order until one returns a non-null value
var result;
coercer.some(function(coercer) {
result = coerce(ctx, coercer, value);
return result != null;
});
return result;
}
coerce.string = function(value) {
if (value != null &&
typeof value === 'object' &&
typeof value.toString === 'function') {
value = value.toString();
}
return typeOf('string', primitive(value));
};
coerce.number = function(value) {
return typeOf('number', primitive(value));
};
coerce.boolean = function(value) {
return typeOf('boolean', primitive(value));
};
coerce.date = function(value) {
value = primitive(value);
if (typeof value === 'number' && !isNaN(value) && isFinite(value)) {
return new Date(value);
}
};
function typeOf(type, value) {
if (typeof value === type) {
return value;
}
}
function primitive(value) {
if (value != null &&
typeof value === 'object' &&
typeof value.valueOf === 'function') {
value = value.valueOf();
}
return value;
}
function slice(value, from) {
return Array.prototype.slice.call(value, from);
}
// Add methods for each type
types.forEach(function(type) {
// Make it an array for easier concat
var typeArg = [type];
normalize[type] = function() {
var args = slice(arguments);
return normalize.apply(this, typeArg.concat(args));
};
});
module.exports = normalize;
value-or-function-3.0.0/package.json 0000664 0000000 0000000 00000002111 13134463307 0017354 0 ustar 00root root 0000000 0000000 {
"name": "value-or-function",
"version": "3.0.0",
"description": "Normalize a value or function, applying extra args to the function",
"author": "Gulp Team (http://gulpjs.com/)",
"contributors": [
"Blaine Bublitz ",
"Hugo Wood "
],
"repository": "gulpjs/value-or-function",
"license": "MIT",
"engines": {
"node": ">= 0.10"
},
"main": "index.js",
"files": [
"index.js"
],
"scripts": {
"lint": "eslint . && jscs index.js test/",
"pretest": "npm run lint",
"test": "mocha --async-only",
"cover": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly",
"coveralls": "npm run cover && istanbul-coveralls"
},
"dependencies": {},
"devDependencies": {
"eslint": "^1.10.3",
"eslint-config-gulp": "^2.0.0",
"expect": "^1.16.0",
"istanbul": "^0.4.3",
"istanbul-coveralls": "^1.0.3",
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"mocha": "^2.4.5"
},
"keywords": [
"options",
"normalize",
"value",
"function"
]
}
value-or-function-3.0.0/test/ 0000775 0000000 0000000 00000000000 13134463307 0016052 5 ustar 00root root 0000000 0000000 value-or-function-3.0.0/test/index.js 0000664 0000000 0000000 00000031745 13134463307 0017531 0 ustar 00root root 0000000 0000000 'use strict';
var expect = require('expect');
var normalize = require('../');
describe('normalize', function() {
it('compares a type and the type of a value', function(done) {
var type = 'string';
var value = 'test string';
var result = normalize(type, value);
expect(result).toBe(value);
done();
});
it('returns undefined if value does not match type', function(done) {
var type = 'string';
var value = 1;
var result = normalize(type, value);
expect(result).toBe(undefined);
done();
});
it('supports arrays for the type parameter', function(done) {
var type = ['string'];
var value = 'test string';
var result = normalize(type, value);
expect(result).toBe(value);
done();
});
it('compares each type and the type of the value', function(done) {
var type = ['number', 'string', 'object'];
var value = 'test string';
var result = normalize(type, value);
expect(result).toBe(value);
done();
});
it('returns undefined if value does not match any type', function(done) {
var type = ['string', 'undefined'];
var value = 1;
var result = normalize(type, value);
expect(result).toBe(undefined);
done();
});
it('supports coercer functions for the type parameter', function(done) {
var type = function() {
return true;
};
var value = 1;
var result = normalize(type, value);
expect(result).toBe(true);
done();
});
it('calls the coercer function to attempt coercion', function(done) {
var expected = 1;
var type = expect.createSpy().andCall(function(value) {
return value;
});
var result = normalize(type, expected);
expect(result).toBe(expected);
expect(type).toHaveBeenCalled();
done();
});
it('calls the coercer functions with context, if bound', function(done) {
var expected = 1;
var context = {};
var type = expect.createSpy().andCall(function(value) {
expect(this).toBe(context);
return value;
});
var result = normalize.call(context, type, expected);
expect(result).toEqual(expected);
expect(type).toHaveBeenCalled();
done();
});
it('calls the value if it is a function', function(done) {
var type = 'string';
var expected = 'test string';
var value = expect.createSpy().andCall(function() {
return expected;
});
var result = normalize(type, value);
expect(result).toBe(expected);
expect(value).toHaveBeenCalled();
done();
});
it('calls the value function with context, if bound', function(done) {
var type = 'string';
var context = {};
var value = expect.createSpy().andCall(function() {
expect(this).toBe(context);
});
normalize.call(context, type, value);
expect(value).toHaveBeenCalled();
done();
});
it('checks the result of function against coercer', function(done) {
var expected = 'test string';
var coercer = expect.createSpy().andCall(function(value) {
return (typeof value === 'string') ? value : undefined;
});
var value = expect.createSpy().andCall(function() {
return expected;
});
var result = normalize(coercer, value);
expect(result).toBe(expected);
expect(coercer).toHaveBeenCalled();
expect(value).toHaveBeenCalled();
done();
});
it('calls the function, passing extra arguments', function(done) {
var type = 'string';
var expected = 'test string';
var value = expect.createSpy().andCall(function(arg) {
return arg;
});
var result = normalize(type, value, expected);
expect(result).toBe(expected);
expect(value).toHaveBeenCalled();
done();
});
it('returns null if result of function does not match type', function(done) {
var type = 'string';
var value = expect.createSpy().andCall(function() {
return 123;
});
var result = normalize(type, value);
expect(result).toBe(undefined);
expect(value).toHaveBeenCalled();
done();
});
it('rejects if function return val doesn\'t satisfy custom coercer', function(done) {
var coercer = expect.createSpy().andCall(function(value) {
return (typeof value === 'string') ? value : undefined;
});
var value = expect.createSpy().andCall(function() {
return 123;
});
var result = normalize(coercer, value);
expect(result).toBe(undefined);
expect(coercer).toHaveBeenCalled();
expect(value).toHaveBeenCalled();
done();
});
});
describe('normalize.object', function() {
it('compares value to typeof object', function(done) {
var obj = {};
var arr = [];
var numObj = new Number(1);
var strObj = new String('test');
var objResult = normalize.object(obj);
var arrResult = normalize.object(arr);
var numObjResult = normalize.object(numObj);
var strObjResult = normalize.object(strObj);
expect(objResult).toBe(obj);
expect(arrResult).toBe(arr);
expect(numObjResult).toBe(numObj);
expect(strObjResult).toBe(strObj);
done();
});
it('accpets value if it is null', function(done) {
var value = null;
var result = normalize.object(value);
expect(result).toBe(null);
done();
});
it('rejects values if not Object', function(done) {
var value = 'invalid';
var result = normalize.object(value);
expect(result).toBe(undefined);
done();
});
it('calls the object function with context, if bound', function(done) {
var context = {};
var value = expect.createSpy().andCall(function() {
expect(this).toBe(context);
});
normalize.object.call(context, value);
expect(value).toHaveBeenCalled();
done();
});
});
describe('normalize.number', function() {
it('accepts value if typeof number', function(done) {
var value = 1;
var result = normalize.number(value);
expect(result).toBe(value);
done();
});
it('accepts value if it is not-a-number', function(done) {
var value = Number.NaN;
var result = normalize.number(value);
expect(Number.isNaN(result)).toBe(true);
done();
});
it('accepts value if it is infinite', function(done) {
var value = Number.NEGATIVE_INFINITY;
var result = normalize.number(value);
expect(result).toBe(value);
done();
});
it('accepts value if instanceof Number', function(done) {
var expected = 1;
var value = new Number(expected);
var result = normalize.number(value);
expect(result).toBe(expected);
done();
});
it('rejects values that won\'t coerce to number', function(done) {
var value = 'invalid';
var result = normalize.number(value);
expect(result).toBe(undefined);
done();
});
it('calls the number function with context, if bound', function(done) {
var context = {};
var value = expect.createSpy().andCall(function() {
expect(this).toBe(context);
});
normalize.number.call(context, value);
expect(value).toHaveBeenCalled();
done();
});
});
describe('normalize.string', function() {
it('accepts value if typeof string', function(done) {
var value = 'test string';
var result = normalize.string(value);
expect(result).toBe(value);
done();
});
it('accepts value if instanceof String', function(done) {
var expected = 'test string';
var value = new String(expected);
var result = normalize.string(value);
expect(result).toBe(expected);
done();
});
it('accepts value if it is an Object', function(done) {
var expected = 'test string';
var value = {
toString: function() {
return expected;
},
};
var result = normalize.string(value);
expect(result).toBe(expected);
done();
});
it('rejects Object if its toString doesn\'t return string', function(done) {
var value = {
toString: function() {
return {};
},
};
var result = normalize.string(value);
expect(result).toBe(undefined);
done();
});
it('rejects values that won\'t coerce to string', function(done) {
var value = undefined;
var result = normalize.string(value);
expect(result).toBe(undefined);
done();
});
it('calls the string function with context, if bound', function(done) {
var context = {};
var value = expect.createSpy().andCall(function() {
expect(this).toBe(context);
});
normalize.string.call(context, value);
expect(value).toHaveBeenCalled();
done();
});
});
describe('normalize.symbol', function() {
it('compares value to typeof symbol', function(done) {
if (!global.Symbol) {
console.log('Only available on platforms that support Symbol');
this.skip();
return;
}
var value = Symbol();
var result = normalize.symbol(value);
expect(result).toBe(value);
done();
});
it('rejects values that are not Symbol', function(done) {
if (!global.Symbol) {
console.log('Only available on platforms that support Symbol');
this.skip();
return;
}
var value = 'invalid';
var result = normalize.symbol(value);
expect(result).toBe(undefined);
done();
});
it('calls the symbol function with context, if bound', function(done) {
if (!global.Symbol) {
console.log('Only available on platforms that support Symbol');
this.skip();
return;
}
var context = {};
var value = expect.createSpy().andCall(function() {
expect(this).toBe(context);
});
normalize.symbol.call(context, value);
expect(value).toHaveBeenCalled();
done();
});
});
describe('normalize.boolean', function() {
it('accepts value if typeof boolean', function(done) {
var value = true;
var result = normalize.boolean(value);
expect(result).toBe(value);
done();
});
it('accepts value if instanceof Boolean', function(done) {
var expected = true;
var value = new Boolean(expected);
var result = normalize.boolean(value);
expect(result).toBe(expected);
done();
});
it('rejects values that won\'t coerce to boolean', function(done) {
var value = 'invalid';
var result = normalize.boolean(value);
expect(result).toBe(undefined);
done();
});
it('calls the boolean function with context, if bound', function(done) {
var context = {};
var value = expect.createSpy().andCall(function() {
expect(this).toBe(context);
});
normalize.boolean.call(context, value);
expect(value).toHaveBeenCalled();
done();
});
});
describe('normalize.function', function() {
it('accepts value if typeof function', function(done) {
var value = function() {};
var result = normalize.function(value);
expect(result).toBe(value);
done();
});
it('never calls the function', function(done) {
var value = expect.createSpy();
var result = normalize.function(value);
expect(result).toBe(value);
expect(value).toNotHaveBeenCalled();
done();
});
it('rejects values that won\'t coerce to function', function(done) {
var value = 'invalid';
var result = normalize.function(value);
expect(result).toBe(undefined);
done();
});
});
describe('normalize.date', function() {
it('coerces a number to a Date object', function(done) {
var value = 1;
var expected = new Date(value);
var result = normalize.date(value);
expect(result).toEqual(expected);
done();
});
it('rejects numbers with not-a-number values', function(done) {
var value = Number.NaN;
var result = normalize.date(value);
expect(result).toBe(undefined);
done();
});
it('rejects numbers with infinite values', function(done) {
var value = Number.POSITIVE_INFINITY;
var result = normalize.date(value);
expect(result).toBe(undefined);
done();
});
it('accepts objects that are Numbers', function(done) {
var value = new Number(1);
var expected = new Date(value);
var result = normalize.date(value);
expect(result).toEqual(expected);
done();
});
it('rejects Numbers with not-a-number values', function(done) {
var value = new Number(Number.NaN);
var result = normalize.date(value);
expect(result).toBe(undefined);
done();
});
it('rejects Numbers with infinite values', function(done) {
var value = new Number(Number.POSITIVE_INFINITY);
var result = normalize.date(value);
expect(result).toBe(undefined);
done();
});
it('accepts objects that are valid Dates', function(done) {
var value = new Date();
var result = normalize.date(value);
expect(result).toEqual(value);
done();
});
it('rejects Dates that are invalid', function(done) {
var value = new Date(undefined);
var result = normalize.date(value);
expect(result).toBe(undefined);
done();
});
it('rejects object that are not dates', function(done) {
var value = 'invalid';
var result = normalize.date(value);
expect(result).toBe(undefined);
done();
});
it('calls the date function with context, if bound', function(done) {
var context = {};
var value = expect.createSpy().andCall(function() {
expect(this).toBe(context);
});
normalize.date.call(context, value);
expect(value).toHaveBeenCalled();
done();
});
});