Skip to content

Commit

Permalink
Merge pull request #13 from lucono/interface_methods_for_is_api
Browse files Browse the repository at this point in the history
Interface methods for `is` API (issue #12).
  • Loading branch information
lucono committed Dec 11, 2016
2 parents 3e04cd9 + 30dcdb0 commit 8a73b3a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,7 @@ language: node_js
node_js:
- "0.10"
- "0.12"
- "node"
- "6.9.2"
before_script:
- export CHROME_BIN=chromium-browser
- npm install -g grunt-cli
Expand Down
47 changes: 29 additions & 18 deletions project/xtypejs/dist/xtype.js
@@ -1,4 +1,4 @@
/** @license | xtypejs v0.6.1 | (c) 2015, Lucas Ononiwu | MIT license, xtype.js.org/license.txt
/** @license | xtypejs v0.7.0 | (c) 2015, Lucas Ononiwu | MIT license, xtype.js.org/license.txt
*/

/**
Expand Down Expand Up @@ -34,7 +34,7 @@
*/

var LIB_NAME = 'xtype',
LIB_VERSION = '0.6.1',
LIB_VERSION = '0.7.0',

TYPE_DELIMITER_DEFAULT_PATTERN = '[|, ]',
NAME_SCHEME_DEFAULT_OPTION_VALUE = 'default',
Expand Down Expand Up @@ -502,6 +502,23 @@
clearTypeListStringCache();
}

function defineInterfacePackagesAndMethods(hostObj) {
hostObj.not = newObj();
hostObj.any = newObj();
hostObj.all = newObj();
hostObj.some = newObj();
hostObj.none = newObj();

hostObj.not.is = function(value, types) {
return !isType(value, types);
};

hostObj.any.is = getInterfaceFunction(isType, true, true, undefined, true);
hostObj.all.is = getInterfaceFunction(isType, true, undefined, true, false);
hostObj.some.is = getInterfaceFunction(isType, true, true, true, true);
hostObj.none.is = getInterfaceFunction(isType, true, true, undefined, false);
}

/**
* Defines the typeId property and associated type check
* and interface methods for the specified type.
Expand Down Expand Up @@ -530,22 +547,14 @@

hostObj[typeMethodName] = typeCheckFunction;

hostObj.not = (hostObj.not || newObj());
hostObj.not[typeMethodName] = function(value) {
return !typeCheckFunction(value);
};

hostObj.any = (hostObj.any || newObj());
hostObj.any[typeMethodName] = getInterfaceFunction(typeCheckFunction, true, undefined, true);

hostObj.all = (hostObj.all || newObj());
hostObj.all[typeMethodName] = getInterfaceFunction(typeCheckFunction, undefined, true, false);

hostObj.some = (hostObj.some || newObj());
hostObj.some[typeMethodName] = getInterfaceFunction(typeCheckFunction, true, true, true);

hostObj.none = (hostObj.none || newObj());
hostObj.none[typeMethodName] = getInterfaceFunction(typeCheckFunction, true, undefined, false);
hostObj.any[typeMethodName] = getInterfaceFunction(typeCheckFunction, false, true, undefined, true);
hostObj.all[typeMethodName] = getInterfaceFunction(typeCheckFunction, false, undefined, true, false);
hostObj.some[typeMethodName] = getInterfaceFunction(typeCheckFunction, false, true, true, true);
hostObj.none[typeMethodName] = getInterfaceFunction(typeCheckFunction, false, true, undefined, false);
}

/**
Expand All @@ -570,9 +579,9 @@
/**
* Creates an interface function using the specified parameters.
*/
function getInterfaceFunction(delegateFunction, trueCondition, falseCondition, terminationResult) {
return function(values) {
values = (arguments.length > 1 ? arraySlice.call(arguments)
function getInterfaceFunction(delegateFunction, withTypes, trueCondition, falseCondition, terminationResult) {
return function(values, types) {
values = (!withTypes && arguments.length > 1 ? arraySlice.call(arguments)
: isArray(values) ? values
: [values]);

Expand All @@ -581,7 +590,7 @@
valueIndex;

for (valueIndex = 0; valueIndex < values.length; valueIndex++) {
if (delegateFunction(values[valueIndex])) {
if (delegateFunction(values[valueIndex], types)) {
trueResult = true;
} else {
falseResult = true;
Expand Down Expand Up @@ -820,6 +829,8 @@
objToStringToNameMapping['[object ' + objectType + ']'] = objectType.toLowerCase();
});

defineInterfacePackagesAndMethods(moduleExport);

objKeys(TYPE_VALUE_MAPPING).forEach(function(typeName) {
defineType(typeName, TYPE_VALUE_MAPPING[typeName], moduleExport);
});
Expand Down
2 changes: 1 addition & 1 deletion project/xtypejs/dist/xtype.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions project/xtypejs/dist/xtype.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions project/xtypejs/package.json
@@ -1,8 +1,8 @@
{
"name": "xtypejs",
"version": "0.6.1",
"version": "0.7.0",
"description": "Elegant, highly efficient data validation for JavaScript.",
"main": "dist/xtype",
"main": "dist/xtype.js",
"keywords": [
"type",
"xtype",
Expand Down

0 comments on commit 8a73b3a

Please sign in to comment.