Comparing version 1.9.5 to 2.0.1
{ | ||
"name": "types.js", | ||
"version": "1.9.5", | ||
"version": "2.0.1", | ||
"description": "A tiny and fast dynamic type checker/enforcer library", | ||
"main": "types.min.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"uglify": "terser types.js -c -m -o types.min.js", | ||
"uglify": "terser types.js -c -m -o index.js", | ||
"build": "coffee -c --transpile types.coffee && npm run uglify", | ||
@@ -40,4 +40,5 @@ "test": "jasmine" | ||
"@babel/preset-env": "latest", | ||
"jasmine": "^4.1.0" | ||
"jasmine": "^4.1.0", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
types.js | ||
======== | ||
<br/> | ||
![npm](https://img.shields.io/npm/v/types.js) | ||
![npm bundle size](https://img.shields.io/bundlephobia/min/types.js) | ||
![Static Badge](https://img.shields.io/badge/code_coverage-100%25-darkgreen) | ||
![Static Badge](https://img.shields.io/badge/supports-TypeScript-orange) | ||
![GitHub](https://img.shields.io/github/license/phazelift/types.js) | ||
### A tiny and fast dynamic type checker/enforcer library. | ||
@@ -17,3 +23,3 @@ | ||
With reduced size and optimized for speed, the performance penalty is negligible for the majority of operations. It's roughly between 0.00002ms and 0.0001ms per call (on my 4yr old laptop). | ||
With reduced size and optimized for speed, the performance penalty is negligible for the majority of operations. | ||
@@ -25,7 +31,8 @@ <br/> | ||
- fixes NaN, array, null, etc.. | ||
- can check one or multiple values at once | ||
- 4 convenience forms: is[type], not[type], has[type] and all[type] | ||
- can check multiple values at once | ||
- 5 convenience forms: is[type], not[type], has[type], all[type], getFirst[type] | ||
- provides a typeof replacement for proper and reliable type assessment | ||
- you can force/ensure any return value to be of any specific type | ||
- create basic immutable 'enum' objects | ||
- support for TypeScript | ||
- battle tested | ||
@@ -573,2 +580,3 @@ | ||
forceRegExp | /(?:)/ | ||
forceDate | new Date | ||
___ | ||
@@ -579,2 +587,9 @@ change log | ||
**2.0.0** | ||
- adds TypeScript support! | ||
- adds Date literal, now returning: new Date | ||
- fix enum and regexp type not always being properly parsed | ||
- renamed types.min.js -> index.js | ||
**1.9.0** | ||
@@ -637,12 +652,7 @@ | ||
Updated the readme. | ||
___ | ||
**1.3.9** | ||
Removed 'unknown' from types.js type definitions. It was meant to be like a final state, for if no other matching type could | ||
be found, but in the codebase as it is now, that state can never be reached.. If Javascript ever invents a brand new type, | ||
types.js will return 'defined' on that one if I would not take action and implement support for it. | ||
Removed 'unknown' from types.js type definitions. | ||
Updated the readme. | ||
___ | ||
@@ -649,0 +659,0 @@ **1.3.5** |
126
types.js
"use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } | ||
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } | ||
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } | ||
// Generated by CoffeeScript 2.7.0 | ||
(function () { | ||
var _LITERALS, _TYPES; | ||
// types.js - A tiny and fast dynamic type checker/enforcer library | ||
// types.js - A tiny and fast dynamic type checker/enforcer library | ||
// MIT License | ||
// Copyright (c) 2014 Dennis Raymondo van der Sluis | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
@@ -20,4 +22,6 @@ // of this software and associated documentation files (the "Software"), to deal | ||
// 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 | ||
@@ -30,5 +34,5 @@ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// SOFTWARE. | ||
// heavily refactored to reduce size while optimizing for speed, at the cost of some readability.. | ||
var ARRAY, BOOLEAN, BREAK_IF_EQUAL, DATE, DEFINED, ENUM, ENUM_ERR_PREFIX, ENUM_ID, FORCE_MSG_PREFIX, FUNCTION, LITERALS, LOGGING_DISABLED, LOGGING_ENABLED, MODULE_NAME, NAN, NULL, NUMBER, OBJECT, REGEXP, STRING, SYMBOL, TYPES, Types, UNDEFINED, createEnum, createForce, instanceOf, log, _logForce, testHasAndAll, typeOf, types, upFirst; | ||
MODULE_NAME = 'types.js'; | ||
@@ -59,24 +63,24 @@ ENUM_ID = '_ENUM_'; | ||
return number; | ||
}()), _defineProperty(_LITERALS, REGEXP, new RegExp()), _LITERALS); | ||
}()), _defineProperty(_LITERALS, REGEXP, new RegExp()), _defineProperty(_LITERALS, DATE, new Date()), _LITERALS); | ||
log = function log() { | ||
if (Types.logging) { | ||
var _console; | ||
return (_console = console).log.apply(_console, arguments); | ||
} | ||
}; | ||
_logForce = function logForce() {}; | ||
_logForce = function logForce() {}; // an unsafe internal for making the first character of a type id uppercase | ||
// an unsafe internal for making the first character of a type id uppercase | ||
upFirst = function upFirst(str) { | ||
return str = str[0].toUpperCase() + str.slice(1); | ||
if (str === 'regexp') { | ||
return 'RegExp'; | ||
} else { | ||
return str[0].toUpperCase() + str.slice(1); | ||
} | ||
}; | ||
instanceOf = function instanceOf(type, value) { | ||
return value instanceof type; | ||
}; // type defaults to object, for internal can do, saves for a few bytes.. | ||
}; | ||
// type defaults to object, for internal can do, saves for a few bytes.. | ||
typeOf = function typeOf(value) { | ||
@@ -86,6 +90,4 @@ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : OBJECT; | ||
}; | ||
createEnum = function createEnum() { | ||
var _enum; | ||
_enum = {}; | ||
@@ -99,5 +101,5 @@ Object.defineProperty(_enum, ENUM_ID, { | ||
return _enum; | ||
}; // all definitions that will be used for the is[type], not[type], etc.. tests | ||
}; | ||
// all definitions that will be used for the is[type], not[type], etc.. tests | ||
TYPES = (_TYPES = {}, _defineProperty(_TYPES, UNDEFINED, function (value) { | ||
@@ -121,2 +123,4 @@ return value === void 0; | ||
return typeOf(value, NUMBER) && value === value || typeOf(value) && instanceOf(Number, value); | ||
}), _defineProperty(_TYPES, ENUM, function (value) { | ||
return Types.forceObject(value).hasOwnProperty(ENUM_ID); | ||
}), _defineProperty(_TYPES, OBJECT, function (value) { | ||
@@ -128,13 +132,10 @@ return typeOf(value) && value !== null && !instanceOf(Boolean, value) && !instanceOf(Number, value) && !instanceOf(Array, value) && !instanceOf(RegExp, value) && !instanceOf(Date, value); | ||
return typeOf(value, NUMBER) && value !== value; | ||
}), _defineProperty(_TYPES, ENUM, function (value) { | ||
return Types.forceObject(value).hasOwnProperty(ENUM_ID); | ||
}), _defineProperty(_TYPES, DEFINED, function (value) { | ||
return value !== void 0; | ||
}), _TYPES); | ||
TYPES.StringOrNumber = function (value) { | ||
return TYPES[STRING](value) || TYPES[NUMBER](value); | ||
}; // define the main object that this module returns | ||
}; | ||
// define the main object that this module returns | ||
Types = types = { | ||
@@ -156,3 +157,2 @@ // used by forceNumber to set the Radix, defaults to decimals | ||
var msg; | ||
if (Types.isFunction(externalLog)) { | ||
@@ -162,3 +162,2 @@ return externalLog(errLevel, expectedType, encounteredType); | ||
msg = expectedType; | ||
switch (errLevel) { | ||
@@ -168,11 +167,8 @@ case 1: | ||
break; | ||
case 2: | ||
msg += ": optional value is not of type " + expectedType; | ||
break; | ||
case 3: | ||
msg += ": no valid type found, returning a type " + expectedType + " literal"; | ||
} | ||
return log(FORCE_MSG_PREFIX + msg); | ||
@@ -182,8 +178,9 @@ } | ||
} | ||
}; // factory that creates all Types.force[type] variations | ||
}; | ||
// factory that creates all Types.force[type] variations | ||
createForce = function createForce(type) { | ||
var Type, test; | ||
Type = upFirst(type); // convert value in case initial Type test failed. failed conversion returns undefined | ||
Type = upFirst(type); | ||
// convert value in case initial Type test failed. failed conversion returns undefined | ||
test = function test(value) { | ||
@@ -196,5 +193,3 @@ if (types.autoConvert) { | ||
} | ||
break; | ||
case STRING: | ||
@@ -204,24 +199,17 @@ if (types.isNumber(value)) { | ||
} | ||
} | ||
} | ||
if (Types['is' + Type](value)) { | ||
return value; | ||
} | ||
}; // the forctType method, returns the Type's literal or defaultValue if both value and replacement are not compatible | ||
}; | ||
// the forctType method, returns the Type's literal or defaultValue if both value and replacement are not compatible | ||
return function (value, replacement) { | ||
var okValue, replacementType; | ||
if (value != null && void 0 !== (okValue = test(value))) { | ||
return okValue; | ||
} | ||
_logForce(1, Type, value); | ||
if (types.isDefined(replacement)) { | ||
replacementType = types["typeof"](replacement); | ||
if (replacement != null && void 0 !== (replacement = test(replacement))) { | ||
@@ -233,15 +221,12 @@ return replacement; | ||
} | ||
_logForce(3, Type, value); | ||
return LITERALS[type]; | ||
}; | ||
}; // test multiple values(arguments) for a given predicate. returns breakState if predicate is breakState for some value | ||
}; | ||
// test multiple values(arguments) for a given predicate. returns breakState if predicate is breakState for some value | ||
// when no break occured, ! breakState will be returned. | ||
testHasAndAll = function testHasAndAll(predicate, breakState) { | ||
var values = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var i, len, value; | ||
if (values.length < 1) { | ||
@@ -251,6 +236,4 @@ // testing 'has' or 'all' for 'undefined' should return true on calls without arguments | ||
} | ||
for (i = 0, len = values.length; i < len; i++) { | ||
value = values[i]; | ||
if (predicate(value) === breakState) { | ||
@@ -260,13 +243,10 @@ return breakState; | ||
} | ||
return !breakState; | ||
}; // generate all the is/not/has/all/force Types | ||
}; | ||
// generate all the is/not/has/all/force Types | ||
BREAK_IF_EQUAL = true; | ||
(function () { | ||
var predicate, results, type; | ||
results = []; | ||
for (type in TYPES) { | ||
@@ -276,3 +256,2 @@ predicate = TYPES[type]; | ||
var Type; | ||
Type = function () { | ||
@@ -282,6 +261,4 @@ switch (type) { | ||
return 'NaN'; | ||
case REGEXP: | ||
return 'RegExp'; | ||
default: | ||
@@ -291,32 +268,23 @@ return upFirst(type); | ||
}(); | ||
Types['is' + Type] = predicate; | ||
Types['not' + Type] = function (value) { | ||
return !predicate(value); | ||
}; | ||
Types['has' + Type] = function () { | ||
return testHasAndAll(predicate, BREAK_IF_EQUAL, arguments); | ||
}; | ||
Types['all' + Type] = function () { | ||
return testHasAndAll(predicate, !BREAK_IF_EQUAL, arguments); | ||
}; // create only forceType of types found in LITERALS | ||
}; | ||
// create only forceType of types found in LITERALS | ||
if (type in LITERALS) { | ||
Types['force' + Type] = createForce(type); | ||
} | ||
return Types['getFirst' + Type] = function () { | ||
var i, len, value; | ||
for (var _len = arguments.length, values = new Array(_len), _key = 0; _key < _len; _key++) { | ||
values[_key] = arguments[_key]; | ||
} | ||
for (i = 0, len = values.length; i < len; i++) { | ||
value = values[i]; | ||
if (Types['is' + Type](value)) { | ||
@@ -329,6 +297,4 @@ return value; | ||
} | ||
return results; | ||
})(); | ||
Types.intoArray = function () { | ||
@@ -338,3 +304,2 @@ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
} | ||
if (args.length < 2) { | ||
@@ -348,12 +313,8 @@ if (types.isString(args[0])) { | ||
} | ||
return args; | ||
}; | ||
Types.typeOf = Types["typeof"] = function (value) { | ||
var name, predicate; | ||
for (name in TYPES) { | ||
predicate = TYPES[name]; | ||
if (predicate(value) === true) { | ||
@@ -364,9 +325,6 @@ return name; | ||
}; | ||
Types["enum"] = Types.enumerate = function (items, offset) { | ||
var _enum, i, index, item, len; | ||
offset = types.forceNumber(offset, 0); | ||
_enum = createEnum(); | ||
if (types.notArray(items)) { | ||
@@ -376,6 +334,4 @@ log("".concat(ENUM_ERR_PREFIX, " invalid or missing enumeration array")); | ||
} | ||
for (index = i = 0, len = items.length; i < len; index = ++i) { | ||
item = items[index]; | ||
if (types.isString(item)) { | ||
@@ -387,7 +343,5 @@ _enum[item] = index + offset; | ||
} | ||
types.forceFunction(Object.freeze)(_enum); | ||
return _enum; | ||
}; | ||
if (typeof define !== "undefined" && define !== null && FUNCTION === (typeof define === "undefined" ? "undefined" : _typeof(define)) && define.amd) { | ||
@@ -398,7 +352,5 @@ define('types', [], function () { | ||
} | ||
if ((typeof module === "undefined" ? "undefined" : _typeof(module)) !== UNDEFINED) { | ||
module.exports = Types; | ||
} | ||
if ((typeof window === "undefined" ? "undefined" : _typeof(window)) !== UNDEFINED) { | ||
@@ -405,0 +357,0 @@ window.Types = Types; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
141722
10
2419
706
0
4