265 lines
5.6 KiB
JavaScript
265 lines
5.6 KiB
JavaScript
'use strict';
|
|
|
|
var SafeString = require('./runtime').SafeString;
|
|
|
|
/**
|
|
* Returns `true` if the object is a function, otherwise `false`.
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function callable(value) {
|
|
return typeof value === 'function';
|
|
}
|
|
exports.callable = callable;
|
|
|
|
/**
|
|
* Returns `true` if the object is strictly not `undefined`.
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function defined(value) {
|
|
return value !== undefined;
|
|
}
|
|
exports.defined = defined;
|
|
|
|
/**
|
|
* Returns `true` if the operand (one) is divisble by the test's argument
|
|
* (two).
|
|
* @param { number } one
|
|
* @param { number } two
|
|
* @returns { boolean }
|
|
*/
|
|
function divisibleby(one, two) {
|
|
return one % two === 0;
|
|
}
|
|
exports.divisibleby = divisibleby;
|
|
|
|
/**
|
|
* Returns true if the string has been escaped (i.e., is a SafeString).
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function escaped(value) {
|
|
return value instanceof SafeString;
|
|
}
|
|
exports.escaped = escaped;
|
|
|
|
/**
|
|
* Returns `true` if the arguments are strictly equal.
|
|
* @param { any } one
|
|
* @param { any } two
|
|
*/
|
|
function equalto(one, two) {
|
|
return one === two;
|
|
}
|
|
exports.equalto = equalto;
|
|
|
|
// Aliases
|
|
exports.eq = exports.equalto;
|
|
exports.sameas = exports.equalto;
|
|
|
|
/**
|
|
* Returns `true` if the value is evenly divisible by 2.
|
|
* @param { number } value
|
|
* @returns { boolean }
|
|
*/
|
|
function even(value) {
|
|
return value % 2 === 0;
|
|
}
|
|
exports.even = even;
|
|
|
|
/**
|
|
* Returns `true` if the value is falsy - if I recall correctly, '', 0, false,
|
|
* undefined, NaN or null. I don't know if we should stick to the default JS
|
|
* behavior or attempt to replicate what Python believes should be falsy (i.e.,
|
|
* empty arrays, empty dicts, not 0...).
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function falsy(value) {
|
|
return !value;
|
|
}
|
|
exports.falsy = falsy;
|
|
|
|
/**
|
|
* Returns `true` if the operand (one) is greater or equal to the test's
|
|
* argument (two).
|
|
* @param { number } one
|
|
* @param { number } two
|
|
* @returns { boolean }
|
|
*/
|
|
function ge(one, two) {
|
|
return one >= two;
|
|
}
|
|
exports.ge = ge;
|
|
|
|
/**
|
|
* Returns `true` if the operand (one) is greater than the test's argument
|
|
* (two).
|
|
* @param { number } one
|
|
* @param { number } two
|
|
* @returns { boolean }
|
|
*/
|
|
function greaterthan(one, two) {
|
|
return one > two;
|
|
}
|
|
exports.greaterthan = greaterthan;
|
|
|
|
// alias
|
|
exports.gt = exports.greaterthan;
|
|
|
|
/**
|
|
* Returns `true` if the operand (one) is less than or equal to the test's
|
|
* argument (two).
|
|
* @param { number } one
|
|
* @param { number } two
|
|
* @returns { boolean }
|
|
*/
|
|
function le(one, two) {
|
|
return one <= two;
|
|
}
|
|
exports.le = le;
|
|
|
|
/**
|
|
* Returns `true` if the operand (one) is less than the test's passed argument
|
|
* (two).
|
|
* @param { number } one
|
|
* @param { number } two
|
|
* @returns { boolean }
|
|
*/
|
|
function lessthan(one, two) {
|
|
return one < two;
|
|
}
|
|
exports.lessthan = lessthan;
|
|
|
|
// alias
|
|
exports.lt = exports.lessthan;
|
|
|
|
/**
|
|
* Returns `true` if the string is lowercased.
|
|
* @param { string } value
|
|
* @returns { boolean }
|
|
*/
|
|
function lower(value) {
|
|
return value.toLowerCase() === value;
|
|
}
|
|
exports.lower = lower;
|
|
|
|
/**
|
|
* Returns `true` if the operand (one) is less than or equal to the test's
|
|
* argument (two).
|
|
* @param { number } one
|
|
* @param { number } two
|
|
* @returns { boolean }
|
|
*/
|
|
function ne(one, two) {
|
|
return one !== two;
|
|
}
|
|
exports.ne = ne;
|
|
|
|
/**
|
|
* Returns true if the value is strictly equal to `null`.
|
|
* @param { any }
|
|
* @returns { boolean }
|
|
*/
|
|
function nullTest(value) {
|
|
return value === null;
|
|
}
|
|
exports.null = nullTest;
|
|
|
|
/**
|
|
* Returns true if value is a number.
|
|
* @param { any }
|
|
* @returns { boolean }
|
|
*/
|
|
function number(value) {
|
|
return typeof value === 'number';
|
|
}
|
|
exports.number = number;
|
|
|
|
/**
|
|
* Returns `true` if the value is *not* evenly divisible by 2.
|
|
* @param { number } value
|
|
* @returns { boolean }
|
|
*/
|
|
function odd(value) {
|
|
return value % 2 === 1;
|
|
}
|
|
exports.odd = odd;
|
|
|
|
/**
|
|
* Returns `true` if the value is a string, `false` if not.
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function string(value) {
|
|
return typeof value === 'string';
|
|
}
|
|
exports.string = string;
|
|
|
|
/**
|
|
* Returns `true` if the value is not in the list of things considered falsy:
|
|
* '', null, undefined, 0, NaN and false.
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function truthy(value) {
|
|
return !!value;
|
|
}
|
|
exports.truthy = truthy;
|
|
|
|
/**
|
|
* Returns `true` if the value is undefined.
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function undefinedTest(value) {
|
|
return value === undefined;
|
|
}
|
|
exports.undefined = undefinedTest;
|
|
|
|
/**
|
|
* Returns `true` if the string is uppercased.
|
|
* @param { string } value
|
|
* @returns { boolean }
|
|
*/
|
|
function upper(value) {
|
|
return value.toUpperCase() === value;
|
|
}
|
|
exports.upper = upper;
|
|
|
|
/**
|
|
* If ES6 features are available, returns `true` if the value implements the
|
|
* `Symbol.iterator` method. If not, it's a string or Array.
|
|
*
|
|
* Could potentially cause issues if a browser exists that has Set and Map but
|
|
* not Symbol.
|
|
*
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function iterable(value) {
|
|
if (typeof Symbol !== 'undefined') {
|
|
return !!value[Symbol.iterator];
|
|
} else {
|
|
return Array.isArray(value) || typeof value === 'string';
|
|
}
|
|
}
|
|
exports.iterable = iterable;
|
|
|
|
/**
|
|
* If ES6 features are available, returns `true` if the value is an object hash
|
|
* or an ES6 Map. Otherwise just return if it's an object hash.
|
|
* @param { any } value
|
|
* @returns { boolean }
|
|
*/
|
|
function mapping(value) {
|
|
// only maps and object hashes
|
|
var bool = value !== null && value !== undefined && typeof value === 'object' && !Array.isArray(value);
|
|
if (Set) {
|
|
return bool && !(value instanceof Set);
|
|
} else {
|
|
return bool;
|
|
}
|
|
}
|
|
exports.mapping = mapping; |