vest-utils
Advanced tools
Comparing version 0.0.5 to 0.1.0-dev-405df5
@@ -6,18 +6,12 @@ 'use strict'; | ||
function bindNot(fn) { | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return !fn.apply(void 0, args); | ||
}; | ||
return (...args) => !fn(...args); | ||
} | ||
function isNumeric(value) { | ||
var str = String(value); | ||
var num = Number(value); | ||
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num); | ||
const str = String(value); | ||
const num = Number(value); | ||
const result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num); | ||
return Boolean(result); | ||
} | ||
var isNotNumeric = bindNot(isNumeric); | ||
const isNotNumeric = bindNot(isNumeric); | ||
@@ -27,3 +21,3 @@ function numberEquals(value, eq) { | ||
} | ||
var numberNotEquals = bindNot(numberEquals); | ||
const numberNotEquals = bindNot(numberEquals); | ||
@@ -33,3 +27,3 @@ function lengthEquals(value, arg1) { | ||
} | ||
var lengthNotEquals = bindNot(lengthEquals); | ||
const lengthNotEquals = bindNot(lengthEquals); | ||
@@ -47,11 +41,10 @@ function greaterThan(value, gt) { | ||
*/ | ||
function createCache(maxSize) { | ||
if (maxSize === void 0) { maxSize = 1; } | ||
var cacheStorage = []; | ||
var cache = function (deps, cacheAction) { | ||
var cacheHit = cache.get(deps); | ||
function createCache(maxSize = 1) { | ||
const cacheStorage = []; | ||
const cache = (deps, cacheAction) => { | ||
const cacheHit = cache.get(deps); | ||
// cache hit is not null | ||
if (cacheHit) | ||
return cacheHit[1]; | ||
var result = cacheAction(); | ||
const result = cacheAction(); | ||
cacheStorage.unshift([deps.concat(), result]); | ||
@@ -63,4 +56,4 @@ if (longerThan(cacheStorage, maxSize)) | ||
// invalidate an item in the cache by its dependencies | ||
cache.invalidate = function (deps) { | ||
var index = findIndex(deps); | ||
cache.invalidate = (deps) => { | ||
const index = findIndex(deps); | ||
if (index > -1) | ||
@@ -70,12 +63,7 @@ cacheStorage.splice(index, 1); | ||
// Retrieves an item from the cache. | ||
cache.get = function (deps) { | ||
return cacheStorage[findIndex(deps)] || null; | ||
}; | ||
cache.get = (deps) => cacheStorage[findIndex(deps)] || null; | ||
return cache; | ||
function findIndex(deps) { | ||
return cacheStorage.findIndex(function (_a) { | ||
var cachedDeps = _a[0]; | ||
return lengthEquals(deps, cachedDeps.length) && | ||
deps.every(function (dep, i) { return dep === cachedDeps[i]; }); | ||
}); | ||
return cacheStorage.findIndex(([cachedDeps]) => lengthEquals(deps, cachedDeps.length) && | ||
deps.every((dep, i) => dep === cachedDeps[i])); | ||
} | ||
@@ -87,3 +75,3 @@ } | ||
} | ||
var isNotNull = bindNot(isNull); | ||
const isNotNull = bindNot(isNull); | ||
@@ -93,3 +81,3 @@ function isUndefined(value) { | ||
} | ||
var isNotUndefined = bindNot(isUndefined); | ||
const isNotUndefined = bindNot(isUndefined); | ||
@@ -99,3 +87,3 @@ function isNullish(value) { | ||
} | ||
var isNotNullish = bindNot(isNullish); | ||
const isNotNullish = bindNot(isNullish); | ||
@@ -110,8 +98,4 @@ function asArray(possibleArg) { | ||
function optionalFunctionValue(value) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return isFunction(value) ? value.apply(void 0, args) : value; | ||
function optionalFunctionValue(value, ...args) { | ||
return isFunction(value) ? value(...args) : value; | ||
} | ||
@@ -130,6 +114,6 @@ | ||
} | ||
var isNotArray = bindNot(isArray); | ||
const isNotArray = bindNot(isArray); | ||
function last(values) { | ||
var valuesArray = asArray(values); | ||
const valuesArray = asArray(values); | ||
return valuesArray[valuesArray.length - 1]; | ||
@@ -142,5 +126,4 @@ } | ||
function transform(array, cb) { | ||
var res = []; | ||
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) { | ||
var v = array_1[_i]; | ||
const res = []; | ||
for (const v of array) { | ||
if (isArray(v)) { | ||
@@ -150,3 +133,3 @@ res.push(transform(v, cb)); | ||
else { | ||
var output = cb(v); | ||
const output = cb(v); | ||
if (isNotNull(output)) { | ||
@@ -163,3 +146,3 @@ res.push(output); | ||
function setValueAtPath(array, path, value) { | ||
var current = getCurrent(array, path); | ||
const current = getCurrent(array, path); | ||
current[last(path)] = value; | ||
@@ -169,3 +152,3 @@ return array; | ||
function flatten(values) { | ||
return asArray(values).reduce(function (acc, value) { | ||
return asArray(values).reduce((acc, value) => { | ||
if (isArray(value)) { | ||
@@ -178,5 +161,4 @@ return acc.concat(flatten(value)); | ||
function getCurrent(array, path) { | ||
var current = array; | ||
for (var _i = 0, _a = path.slice(0, -1); _i < _a.length; _i++) { | ||
var p = _a[_i]; | ||
let current = array; | ||
for (const p of path.slice(0, -1)) { | ||
current[p] = defaultTo(current[p], []); | ||
@@ -198,3 +180,3 @@ current = current[p]; | ||
function callEach(arr) { | ||
return arr.forEach(function (fn) { return fn(); }); | ||
return arr.forEach(fn => fn()); | ||
} | ||
@@ -246,3 +228,3 @@ | ||
function deferThrow(message) { | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
throw new Error(message); | ||
@@ -253,17 +235,17 @@ }, 0); | ||
function createBus() { | ||
var listeners = {}; | ||
const listeners = {}; | ||
return { | ||
emit: function (event, data) { | ||
listener(event).forEach(function (handler) { | ||
emit(event, data) { | ||
listener(event).forEach(handler => { | ||
handler(data); | ||
}); | ||
}, | ||
on: function (event, handler) { | ||
on(event, handler) { | ||
listeners[event] = listener(event).concat(handler); | ||
return { | ||
off: function () { | ||
listeners[event] = listener(event).filter(function (h) { return h !== handler; }); | ||
} | ||
off() { | ||
listeners[event] = listener(event).filter(h => h !== handler); | ||
}, | ||
}; | ||
} | ||
}, | ||
}; | ||
@@ -283,10 +265,11 @@ function listener(event) { | ||
*/ | ||
var seq = (function (n) { return function () { | ||
return "".concat(n++); | ||
}; })(0); | ||
const seq = genSeq(); | ||
function genSeq(namespace) { | ||
return ((n) => () => `${namespace ? namespace + '_' : ''}${n++}`)(0); | ||
} | ||
function mapFirst(array, callback) { | ||
var broke = false; | ||
var breakoutValue = null; | ||
for (var i = 0; i < array.length; i++) { | ||
let broke = false; | ||
let breakoutValue = null; | ||
for (let i = 0; i < array.length; i++) { | ||
callback(array[i], breakout, i); | ||
@@ -317,3 +300,3 @@ if (broke) { | ||
} | ||
var isNotEmpty = bindNot(isEmpty); | ||
const isNotEmpty = bindNot(isEmpty); | ||
@@ -324,2 +307,19 @@ function isPositive(value) { | ||
function createTinyState(initialValue) { | ||
let value; | ||
resetValue(); | ||
return () => [value, setValue, resetValue]; | ||
function setValue(nextValue) { | ||
value = optionalFunctionValue(nextValue, value); | ||
} | ||
function resetValue() { | ||
setValue(optionalFunctionValue(initialValue)); | ||
} | ||
} | ||
var tinyState = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
createTinyState: createTinyState | ||
}); | ||
exports.StringObject = StringObject; | ||
@@ -335,2 +335,3 @@ exports.asArray = asArray; | ||
exports.either = either; | ||
exports.genSeq = genSeq; | ||
exports.greaterThan = greaterThan; | ||
@@ -366,1 +367,2 @@ exports.hasOwnProperty = hasOwnProperty; | ||
exports.seq = seq; | ||
exports.tinyState = tinyState; |
@@ -1,1 +0,1 @@ | ||
"use strict";function r(r){return function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return!r.apply(void 0,t)}}function t(r){var t=String(r),n=Number(r),e=!isNaN(parseFloat(t))&&!isNaN(Number(r))&&isFinite(n);return Boolean(e)}Object.defineProperty(exports,"__esModule",{value:!0});var n=r(t);function e(r,n){return t(r)&&t(n)&&Number(r)===Number(n)}var o=r(e);function u(r,t){return e(r.length,t)}var i=r(u);function s(r,n){return t(r)&&t(n)&&Number(r)>Number(n)}function c(r,t){return s(r.length,t)}function a(r){return null===r}var f=r(a);function l(r){return void 0===r}var p=r(l);function x(r){return a(r)||l(r)}var v=r(x);function h(r){return[].concat(r)}function g(r){return"function"==typeof r}function N(r){for(var t=[],n=1;n<arguments.length;n++)t[n-1]=arguments[n];return g(r)?r.apply(void 0,t):r}function d(r,t){var n;return null!==(n=N(r))&&void 0!==n?n:N(t)}function b(r){return Boolean(Array.isArray(r))}var y=r(b);function m(r){var t=h(r);return t[t.length-1]}function E(r,t){for(var n=r,e=0,o=t.slice(0,-1);e<o.length;e++){var u=o[e];n[u]=d(n[u],[]),n=n[u]}return n}var O=Object.freeze({__proto__:null,transform:function r(t,n){for(var e=[],o=0,u=t;o<u.length;o++){var i=u[o];if(b(i))e.push(r(i,n));else{var s=n(i);f(s)&&e.push(s)}}return e},valueAtPath:function(r,t){return E(r,t)[m(t)]},setValueAtPath:function(r,t,n){return E(r,t)[m(t)]=n,r},flatten:function r(t){return h(t).reduce((function(t,n){return b(n)?t.concat(r(n)):h(t).concat(n)}),[])},getCurrent:E});function _(r,t){return Object.prototype.hasOwnProperty.call(r,t)}var j=Object.assign;var w,A=Object.freeze({__proto__:null,createBus:function(){var r={};return{emit:function(r,n){t(r).forEach((function(r){r(n)}))},on:function(n,e){return r[n]=t(n).concat(e),{off:function(){r[n]=t(n).filter((function(r){return r!==e}))}}}};function t(t){return r[t]||[]}}}),P=(w=0,function(){return"".concat(w++)});function S(r){return!r||(_(r,"length")?u(r,0):"object"==typeof r&&u(Object.keys(r),0))}var q=r(S);exports.StringObject=function(r){return new String(N(r))},exports.asArray=h,exports.assign=j,exports.bindNot=r,exports.bus=A,exports.cache=function(r){void 0===r&&(r=1);var t=[],n=function(e,o){var u=n.get(e);if(u)return u[1];var i=o();return t.unshift([e.concat(),i]),c(t,r)&&(t.length=r),i};return n.invalidate=function(r){var n=e(r);n>-1&&t.splice(n,1)},n.get=function(r){return t[e(r)]||null},n;function e(r){return t.findIndex((function(t){var n=t[0];return u(r,n.length)&&r.every((function(r,t){return r===n[t]}))}))}},exports.callEach=function(r){return r.forEach((function(r){return r()}))},exports.defaultTo=d,exports.deferThrow=function(r){setTimeout((function(){throw new Error(r)}),0)},exports.either=function(r,t){return!!r!=!!t},exports.greaterThan=s,exports.hasOwnProperty=_,exports.invariant=function(r,t){if(!r)throw t instanceof String?t.valueOf():new Error(t?N(t):t)},exports.isArray=b,exports.isBoolean=function(r){return!!r===r},exports.isEmpty=S,exports.isFunction=g,exports.isNotArray=y,exports.isNotEmpty=q,exports.isNotNull=f,exports.isNotNullish=v,exports.isNotNumeric=n,exports.isNotUndefined=p,exports.isNull=a,exports.isNullish=x,exports.isNumeric=t,exports.isPositive=function(r){return s(r,0)},exports.isPromise=function(r){return r&&g(r.then)},exports.isStringValue=function(r){return String(r)===r},exports.isUndefined=l,exports.last=m,exports.lengthEquals=u,exports.lengthNotEquals=i,exports.longerThan=c,exports.mapFirst=function(r,t){for(var n=!1,e=null,o=0;o<r.length;o++)if(t(r[o],u,o),n)return e;function u(r,t){r&&(n=!0,e=t)}},exports.nestedArray=O,exports.numberEquals=e,exports.numberNotEquals=o,exports.optionalFunctionValue=N,exports.seq=P; | ||
"use strict";function t(t){return(...n)=>!t(...n)}function n(t){const n=String(t),r=Number(t),e=!isNaN(parseFloat(n))&&!isNaN(Number(t))&&isFinite(r);return Boolean(e)}Object.defineProperty(exports,"__esModule",{value:!0});const r=t(n);function e(t,r){return n(t)&&n(r)&&Number(t)===Number(r)}const o=t(e);function u(t,n){return e(t.length,n)}const s=t(u);function i(t,r){return n(t)&&n(r)&&Number(t)>Number(r)}function c(t,n){return i(t.length,n)}function f(t){return null===t}const a=t(f);function l(t){return void 0===t}const p=t(l);function x(t){return f(t)||l(t)}const h=t(x);function N(t){return[].concat(t)}function g(t){return"function"==typeof t}function b(t,...n){return g(t)?t(...n):t}function y(t,n){var r;return null!==(r=b(t))&&void 0!==r?r:b(n)}function m(t){return Boolean(Array.isArray(t))}const d=t(m);function v(t){const n=N(t);return n[n.length-1]}function _(t,n){let r=t;for(const t of n.slice(0,-1))r[t]=y(r[t],[]),r=r[t];return r}var E=Object.freeze({__proto__:null,transform:function t(n,r){const e=[];for(const o of n)if(m(o))e.push(t(o,r));else{const t=r(o);a(t)&&e.push(t)}return e},valueAtPath:function(t,n){return _(t,n)[v(n)]},setValueAtPath:function(t,n,r){return _(t,n)[v(n)]=r,t},flatten:function t(n){return N(n).reduce(((n,r)=>m(r)?n.concat(t(r)):N(n).concat(r)),[])},getCurrent:_});function O(t,n){return Object.prototype.hasOwnProperty.call(t,n)}var j=Object.assign;var S=Object.freeze({__proto__:null,createBus:function(){const t={};return{emit(t,r){n(t).forEach((t=>{t(r)}))},on:(r,e)=>(t[r]=n(r).concat(e),{off(){t[r]=n(r).filter((t=>t!==e))}})};function n(n){return t[n]||[]}}});const w=A();function A(t){return n=0,()=>`${t?t+"_":""}${n++}`;var n}function P(t){return!t||(O(t,"length")?u(t,0):"object"==typeof t&&u(Object.keys(t),0))}const q=t(P);var T=Object.freeze({__proto__:null,createTinyState:function(t){let n;return e(),()=>[n,r,e];function r(t){n=b(t,n)}function e(){r(b(t))}}});exports.StringObject=function(t){return new String(b(t))},exports.asArray=N,exports.assign=j,exports.bindNot=t,exports.bus=S,exports.cache=function(t=1){const n=[],r=(e,o)=>{const u=r.get(e);if(u)return u[1];const s=o();return n.unshift([e.concat(),s]),c(n,t)&&(n.length=t),s};return r.invalidate=t=>{const r=e(t);r>-1&&n.splice(r,1)},r.get=t=>n[e(t)]||null,r;function e(t){return n.findIndex((([n])=>u(t,n.length)&&t.every(((t,r)=>t===n[r]))))}},exports.callEach=function(t){return t.forEach((t=>t()))},exports.defaultTo=y,exports.deferThrow=function(t){setTimeout((()=>{throw new Error(t)}),0)},exports.either=function(t,n){return!!t!=!!n},exports.genSeq=A,exports.greaterThan=i,exports.hasOwnProperty=O,exports.invariant=function(t,n){if(!t)throw n instanceof String?n.valueOf():new Error(n?b(n):n)},exports.isArray=m,exports.isBoolean=function(t){return!!t===t},exports.isEmpty=P,exports.isFunction=g,exports.isNotArray=d,exports.isNotEmpty=q,exports.isNotNull=a,exports.isNotNullish=h,exports.isNotNumeric=r,exports.isNotUndefined=p,exports.isNull=f,exports.isNullish=x,exports.isNumeric=n,exports.isPositive=function(t){return i(t,0)},exports.isPromise=function(t){return t&&g(t.then)},exports.isStringValue=function(t){return String(t)===t},exports.isUndefined=l,exports.last=v,exports.lengthEquals=u,exports.lengthNotEquals=s,exports.longerThan=c,exports.mapFirst=function(t,n){let r=!1,e=null;for(let u=0;u<t.length;u++)if(n(t[u],o,u),r)return e;function o(t,n){t&&(r=!0,e=n)}},exports.nestedArray=E,exports.numberEquals=e,exports.numberNotEquals=o,exports.optionalFunctionValue=b,exports.seq=w,exports.tinyState=T; |
function bindNot(fn) { | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return !fn.apply(void 0, args); | ||
}; | ||
return (...args) => !fn(...args); | ||
} | ||
function isNumeric(value) { | ||
var str = String(value); | ||
var num = Number(value); | ||
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num); | ||
const str = String(value); | ||
const num = Number(value); | ||
const result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num); | ||
return Boolean(result); | ||
} | ||
var isNotNumeric = bindNot(isNumeric); | ||
const isNotNumeric = bindNot(isNumeric); | ||
@@ -22,3 +16,3 @@ function numberEquals(value, eq) { | ||
} | ||
var numberNotEquals = bindNot(numberEquals); | ||
const numberNotEquals = bindNot(numberEquals); | ||
@@ -28,3 +22,3 @@ function lengthEquals(value, arg1) { | ||
} | ||
var lengthNotEquals = bindNot(lengthEquals); | ||
const lengthNotEquals = bindNot(lengthEquals); | ||
@@ -42,11 +36,10 @@ function greaterThan(value, gt) { | ||
*/ | ||
function createCache(maxSize) { | ||
if (maxSize === void 0) { maxSize = 1; } | ||
var cacheStorage = []; | ||
var cache = function (deps, cacheAction) { | ||
var cacheHit = cache.get(deps); | ||
function createCache(maxSize = 1) { | ||
const cacheStorage = []; | ||
const cache = (deps, cacheAction) => { | ||
const cacheHit = cache.get(deps); | ||
// cache hit is not null | ||
if (cacheHit) | ||
return cacheHit[1]; | ||
var result = cacheAction(); | ||
const result = cacheAction(); | ||
cacheStorage.unshift([deps.concat(), result]); | ||
@@ -58,4 +51,4 @@ if (longerThan(cacheStorage, maxSize)) | ||
// invalidate an item in the cache by its dependencies | ||
cache.invalidate = function (deps) { | ||
var index = findIndex(deps); | ||
cache.invalidate = (deps) => { | ||
const index = findIndex(deps); | ||
if (index > -1) | ||
@@ -65,12 +58,7 @@ cacheStorage.splice(index, 1); | ||
// Retrieves an item from the cache. | ||
cache.get = function (deps) { | ||
return cacheStorage[findIndex(deps)] || null; | ||
}; | ||
cache.get = (deps) => cacheStorage[findIndex(deps)] || null; | ||
return cache; | ||
function findIndex(deps) { | ||
return cacheStorage.findIndex(function (_a) { | ||
var cachedDeps = _a[0]; | ||
return lengthEquals(deps, cachedDeps.length) && | ||
deps.every(function (dep, i) { return dep === cachedDeps[i]; }); | ||
}); | ||
return cacheStorage.findIndex(([cachedDeps]) => lengthEquals(deps, cachedDeps.length) && | ||
deps.every((dep, i) => dep === cachedDeps[i])); | ||
} | ||
@@ -82,3 +70,3 @@ } | ||
} | ||
var isNotNull = bindNot(isNull); | ||
const isNotNull = bindNot(isNull); | ||
@@ -88,3 +76,3 @@ function isUndefined(value) { | ||
} | ||
var isNotUndefined = bindNot(isUndefined); | ||
const isNotUndefined = bindNot(isUndefined); | ||
@@ -94,3 +82,3 @@ function isNullish(value) { | ||
} | ||
var isNotNullish = bindNot(isNullish); | ||
const isNotNullish = bindNot(isNullish); | ||
@@ -105,8 +93,4 @@ function asArray(possibleArg) { | ||
function optionalFunctionValue(value) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return isFunction(value) ? value.apply(void 0, args) : value; | ||
function optionalFunctionValue(value, ...args) { | ||
return isFunction(value) ? value(...args) : value; | ||
} | ||
@@ -125,6 +109,6 @@ | ||
} | ||
var isNotArray = bindNot(isArray); | ||
const isNotArray = bindNot(isArray); | ||
function last(values) { | ||
var valuesArray = asArray(values); | ||
const valuesArray = asArray(values); | ||
return valuesArray[valuesArray.length - 1]; | ||
@@ -137,5 +121,4 @@ } | ||
function transform(array, cb) { | ||
var res = []; | ||
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) { | ||
var v = array_1[_i]; | ||
const res = []; | ||
for (const v of array) { | ||
if (isArray(v)) { | ||
@@ -145,3 +128,3 @@ res.push(transform(v, cb)); | ||
else { | ||
var output = cb(v); | ||
const output = cb(v); | ||
if (isNotNull(output)) { | ||
@@ -158,3 +141,3 @@ res.push(output); | ||
function setValueAtPath(array, path, value) { | ||
var current = getCurrent(array, path); | ||
const current = getCurrent(array, path); | ||
current[last(path)] = value; | ||
@@ -164,3 +147,3 @@ return array; | ||
function flatten(values) { | ||
return asArray(values).reduce(function (acc, value) { | ||
return asArray(values).reduce((acc, value) => { | ||
if (isArray(value)) { | ||
@@ -173,5 +156,4 @@ return acc.concat(flatten(value)); | ||
function getCurrent(array, path) { | ||
var current = array; | ||
for (var _i = 0, _a = path.slice(0, -1); _i < _a.length; _i++) { | ||
var p = _a[_i]; | ||
let current = array; | ||
for (const p of path.slice(0, -1)) { | ||
current[p] = defaultTo(current[p], []); | ||
@@ -193,3 +175,3 @@ current = current[p]; | ||
function callEach(arr) { | ||
return arr.forEach(function (fn) { return fn(); }); | ||
return arr.forEach(fn => fn()); | ||
} | ||
@@ -241,3 +223,3 @@ | ||
function deferThrow(message) { | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
throw new Error(message); | ||
@@ -248,17 +230,17 @@ }, 0); | ||
function createBus() { | ||
var listeners = {}; | ||
const listeners = {}; | ||
return { | ||
emit: function (event, data) { | ||
listener(event).forEach(function (handler) { | ||
emit(event, data) { | ||
listener(event).forEach(handler => { | ||
handler(data); | ||
}); | ||
}, | ||
on: function (event, handler) { | ||
on(event, handler) { | ||
listeners[event] = listener(event).concat(handler); | ||
return { | ||
off: function () { | ||
listeners[event] = listener(event).filter(function (h) { return h !== handler; }); | ||
} | ||
off() { | ||
listeners[event] = listener(event).filter(h => h !== handler); | ||
}, | ||
}; | ||
} | ||
}, | ||
}; | ||
@@ -278,10 +260,11 @@ function listener(event) { | ||
*/ | ||
var seq = (function (n) { return function () { | ||
return "".concat(n++); | ||
}; })(0); | ||
const seq = genSeq(); | ||
function genSeq(namespace) { | ||
return ((n) => () => `${namespace ? namespace + '_' : ''}${n++}`)(0); | ||
} | ||
function mapFirst(array, callback) { | ||
var broke = false; | ||
var breakoutValue = null; | ||
for (var i = 0; i < array.length; i++) { | ||
let broke = false; | ||
let breakoutValue = null; | ||
for (let i = 0; i < array.length; i++) { | ||
callback(array[i], breakout, i); | ||
@@ -312,3 +295,3 @@ if (broke) { | ||
} | ||
var isNotEmpty = bindNot(isEmpty); | ||
const isNotEmpty = bindNot(isEmpty); | ||
@@ -319,2 +302,19 @@ function isPositive(value) { | ||
export { StringObject, asArray, assign, bindNot, bus, createCache as cache, callEach, defaultTo, deferThrow, either, greaterThan, hasOwnProperty, invariant, isArray, isBoolean, isEmpty, isFunction, isNotArray, isNotEmpty, isNotNull, isNotNullish, isNotNumeric, isNotUndefined, isNull, isNullish, isNumeric, isPositive, isPromise, isStringValue, isUndefined, last, lengthEquals, lengthNotEquals, longerThan, mapFirst, nestedArray, numberEquals, numberNotEquals, optionalFunctionValue, seq }; | ||
function createTinyState(initialValue) { | ||
let value; | ||
resetValue(); | ||
return () => [value, setValue, resetValue]; | ||
function setValue(nextValue) { | ||
value = optionalFunctionValue(nextValue, value); | ||
} | ||
function resetValue() { | ||
setValue(optionalFunctionValue(initialValue)); | ||
} | ||
} | ||
var tinyState = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
createTinyState: createTinyState | ||
}); | ||
export { StringObject, asArray, assign, bindNot, bus, createCache as cache, callEach, defaultTo, deferThrow, either, genSeq, greaterThan, hasOwnProperty, invariant, isArray, isBoolean, isEmpty, isFunction, isNotArray, isNotEmpty, isNotNull, isNotNullish, isNotNumeric, isNotUndefined, isNull, isNullish, isNumeric, isPositive, isPromise, isStringValue, isUndefined, last, lengthEquals, lengthNotEquals, longerThan, mapFirst, nestedArray, numberEquals, numberNotEquals, optionalFunctionValue, seq, tinyState }; |
@@ -1,1 +0,1 @@ | ||
function n(n){return function(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];return!n.apply(void 0,r)}}function r(n){var r=String(n),t=Number(n),u=!isNaN(parseFloat(r))&&!isNaN(Number(n))&&isFinite(t);return Boolean(u)}var t=n(r);function u(n,t){return r(n)&&r(t)&&Number(n)===Number(t)}var e=n(u);function o(n,r){return u(n.length,r)}var i=n(o);function c(n,t){return r(n)&&r(t)&&Number(n)>Number(t)}function f(n,r){return c(n.length,r)}function a(n){void 0===n&&(n=1);var r=[],t=function(u,e){var o=t.get(u);if(o)return o[1];var i=e();return r.unshift([u.concat(),i]),f(r,n)&&(r.length=n),i};return t.invalidate=function(n){var t=u(n);t>-1&&r.splice(t,1)},t.get=function(n){return r[u(n)]||null},t;function u(n){return r.findIndex((function(r){var t=r[0];return o(n,t.length)&&n.every((function(n,r){return n===t[r]}))}))}}function l(n){return null===n}var v=n(l);function h(n){return void 0===n}var s=n(h);function g(n){return l(n)||h(n)}var p=n(g);function b(n){return[].concat(n)}function y(n){return"function"==typeof n}function N(n){for(var r=[],t=1;t<arguments.length;t++)r[t-1]=arguments[t];return y(n)?n.apply(void 0,r):n}function d(n,r){var t;return null!==(t=N(n))&&void 0!==t?t:N(r)}function m(n){return Boolean(Array.isArray(n))}var _=n(m);function O(n){var r=b(n);return r[r.length-1]}function j(n,r){for(var t=n,u=0,e=r.slice(0,-1);u<e.length;u++){var o=e[u];t[o]=d(t[o],[]),t=t[o]}return t}var w=Object.freeze({__proto__:null,transform:function n(r,t){for(var u=[],e=0,o=r;e<o.length;e++){var i=o[e];if(m(i))u.push(n(i,t));else{var c=t(i);v(c)&&u.push(c)}}return u},valueAtPath:function(n,r){return j(n,r)[O(r)]},setValueAtPath:function(n,r,t){return j(n,r)[O(r)]=t,n},flatten:function n(r){return b(r).reduce((function(r,t){return m(t)?r.concat(n(t)):b(r).concat(t)}),[])},getCurrent:j});function A(n){return n.forEach((function(n){return n()}))}function E(n,r){return Object.prototype.hasOwnProperty.call(n,r)}function S(n){return n&&y(n.then)}var B=Object.assign;function P(n,r){if(!n)throw r instanceof String?r.valueOf():new Error(r?N(r):r)}function x(n){return new String(N(n))}function z(n){return String(n)===n}function F(n,r){return!!n!=!!r}function k(n){return!!n===n}function C(n){setTimeout((function(){throw new Error(n)}),0)}var I,T=Object.freeze({__proto__:null,createBus:function(){var n={};return{emit:function(n,t){r(n).forEach((function(n){n(t)}))},on:function(t,u){return n[t]=r(t).concat(u),{off:function(){n[t]=r(t).filter((function(n){return n!==u}))}}}};function r(r){return n[r]||[]}}}),V=(I=0,function(){return"".concat(I++)});function q(n,r){for(var t=!1,u=null,e=0;e<n.length;e++)if(r(n[e],o,e),t)return u;function o(n,r){n&&(t=!0,u=r)}}function D(n){return!n||(E(n,"length")?o(n,0):"object"==typeof n&&o(Object.keys(n),0))}var G=n(D);function H(n){return c(n,0)}export{x as StringObject,b as asArray,B as assign,n as bindNot,T as bus,a as cache,A as callEach,d as defaultTo,C as deferThrow,F as either,c as greaterThan,E as hasOwnProperty,P as invariant,m as isArray,k as isBoolean,D as isEmpty,y as isFunction,_ as isNotArray,G as isNotEmpty,v as isNotNull,p as isNotNullish,t as isNotNumeric,s as isNotUndefined,l as isNull,g as isNullish,r as isNumeric,H as isPositive,S as isPromise,z as isStringValue,h as isUndefined,O as last,o as lengthEquals,i as lengthNotEquals,f as longerThan,q as mapFirst,w as nestedArray,u as numberEquals,e as numberNotEquals,N as optionalFunctionValue,V as seq}; | ||
function n(n){return(...t)=>!n(...t)}function t(n){const t=String(n),r=Number(n),e=!isNaN(parseFloat(t))&&!isNaN(Number(n))&&isFinite(r);return Boolean(e)}const r=n(t);function e(n,r){return t(n)&&t(r)&&Number(n)===Number(r)}const o=n(e);function u(n,t){return e(n.length,t)}const c=n(u);function i(n,r){return t(n)&&t(r)&&Number(n)>Number(r)}function f(n,t){return i(n.length,t)}function s(n=1){const t=[],r=(e,o)=>{const u=r.get(e);if(u)return u[1];const c=o();return t.unshift([e.concat(),c]),f(t,n)&&(t.length=n),c};return r.invalidate=n=>{const r=e(n);r>-1&&t.splice(r,1)},r.get=n=>t[e(n)]||null,r;function e(n){return t.findIndex((([t])=>u(n,t.length)&&n.every(((n,r)=>n===t[r]))))}}function a(n){return null===n}const l=n(a);function h(n){return void 0===n}const g=n(h);function b(n){return a(n)||h(n)}const p=n(b);function _(n){return[].concat(n)}function v(n){return"function"==typeof n}function N(n,...t){return v(n)?n(...t):n}function m(n,t){var r;return null!==(r=N(n))&&void 0!==r?r:N(t)}function y(n){return Boolean(Array.isArray(n))}const O=n(y);function j(n){const t=_(n);return t[t.length-1]}function d(n,t){let r=n;for(const n of t.slice(0,-1))r[n]=m(r[n],[]),r=r[n];return r}var w=Object.freeze({__proto__:null,transform:function n(t,r){const e=[];for(const o of t)if(y(o))e.push(n(o,r));else{const n=r(o);l(n)&&e.push(n)}return e},valueAtPath:function(n,t){return d(n,t)[j(t)]},setValueAtPath:function(n,t,r){return d(n,t)[j(t)]=r,n},flatten:function n(t){return _(t).reduce(((t,r)=>y(r)?t.concat(n(r)):_(t).concat(r)),[])},getCurrent:d});function S(n){return n.forEach((n=>n()))}function A(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function E(n){return n&&v(n.then)}var z=Object.assign;function B(n,t){if(!n)throw t instanceof String?t.valueOf():new Error(t?N(t):t)}function P(n){return new String(N(n))}function x(n){return String(n)===n}function F(n,t){return!!n!=!!t}function T(n){return!!n===n}function $(n){setTimeout((()=>{throw new Error(n)}),0)}var k=Object.freeze({__proto__:null,createBus:function(){const n={};return{emit(n,r){t(n).forEach((n=>{n(r)}))},on:(r,e)=>(n[r]=t(r).concat(e),{off(){n[r]=t(r).filter((n=>n!==e))}})};function t(t){return n[t]||[]}}});const C=I();function I(n){return t=0,()=>`${n?n+"_":""}${t++}`;var t}function V(n,t){let r=!1,e=null;for(let u=0;u<n.length;u++)if(t(n[u],o,u),r)return e;function o(n,t){n&&(r=!0,e=t)}}function q(n){return!n||(A(n,"length")?u(n,0):"object"==typeof n&&u(Object.keys(n),0))}const D=n(q);function G(n){return i(n,0)}var H=Object.freeze({__proto__:null,createTinyState:function(n){let t;return e(),()=>[t,r,e];function r(n){t=N(n,t)}function e(){r(N(n))}}});export{P as StringObject,_ as asArray,z as assign,n as bindNot,k as bus,s as cache,S as callEach,m as defaultTo,$ as deferThrow,F as either,I as genSeq,i as greaterThan,A as hasOwnProperty,B as invariant,y as isArray,T as isBoolean,q as isEmpty,v as isFunction,O as isNotArray,D as isNotEmpty,l as isNotNull,p as isNotNullish,r as isNotNumeric,g as isNotUndefined,a as isNull,b as isNullish,t as isNumeric,G as isPositive,E as isPromise,x as isStringValue,h as isUndefined,j as last,u as lengthEquals,c as lengthNotEquals,f as longerThan,V as mapFirst,w as nestedArray,e as numberEquals,o as numberNotEquals,N as optionalFunctionValue,C as seq,H as tinyState}; |
@@ -8,18 +8,12 @@ (function (global, factory) { | ||
function bindNot(fn) { | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return !fn.apply(void 0, args); | ||
}; | ||
return (...args) => !fn(...args); | ||
} | ||
function isNumeric(value) { | ||
var str = String(value); | ||
var num = Number(value); | ||
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num); | ||
const str = String(value); | ||
const num = Number(value); | ||
const result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num); | ||
return Boolean(result); | ||
} | ||
var isNotNumeric = bindNot(isNumeric); | ||
const isNotNumeric = bindNot(isNumeric); | ||
@@ -29,3 +23,3 @@ function numberEquals(value, eq) { | ||
} | ||
var numberNotEquals = bindNot(numberEquals); | ||
const numberNotEquals = bindNot(numberEquals); | ||
@@ -35,3 +29,3 @@ function lengthEquals(value, arg1) { | ||
} | ||
var lengthNotEquals = bindNot(lengthEquals); | ||
const lengthNotEquals = bindNot(lengthEquals); | ||
@@ -49,11 +43,10 @@ function greaterThan(value, gt) { | ||
*/ | ||
function createCache(maxSize) { | ||
if (maxSize === void 0) { maxSize = 1; } | ||
var cacheStorage = []; | ||
var cache = function (deps, cacheAction) { | ||
var cacheHit = cache.get(deps); | ||
function createCache(maxSize = 1) { | ||
const cacheStorage = []; | ||
const cache = (deps, cacheAction) => { | ||
const cacheHit = cache.get(deps); | ||
// cache hit is not null | ||
if (cacheHit) | ||
return cacheHit[1]; | ||
var result = cacheAction(); | ||
const result = cacheAction(); | ||
cacheStorage.unshift([deps.concat(), result]); | ||
@@ -65,4 +58,4 @@ if (longerThan(cacheStorage, maxSize)) | ||
// invalidate an item in the cache by its dependencies | ||
cache.invalidate = function (deps) { | ||
var index = findIndex(deps); | ||
cache.invalidate = (deps) => { | ||
const index = findIndex(deps); | ||
if (index > -1) | ||
@@ -72,12 +65,7 @@ cacheStorage.splice(index, 1); | ||
// Retrieves an item from the cache. | ||
cache.get = function (deps) { | ||
return cacheStorage[findIndex(deps)] || null; | ||
}; | ||
cache.get = (deps) => cacheStorage[findIndex(deps)] || null; | ||
return cache; | ||
function findIndex(deps) { | ||
return cacheStorage.findIndex(function (_a) { | ||
var cachedDeps = _a[0]; | ||
return lengthEquals(deps, cachedDeps.length) && | ||
deps.every(function (dep, i) { return dep === cachedDeps[i]; }); | ||
}); | ||
return cacheStorage.findIndex(([cachedDeps]) => lengthEquals(deps, cachedDeps.length) && | ||
deps.every((dep, i) => dep === cachedDeps[i])); | ||
} | ||
@@ -89,3 +77,3 @@ } | ||
} | ||
var isNotNull = bindNot(isNull); | ||
const isNotNull = bindNot(isNull); | ||
@@ -95,3 +83,3 @@ function isUndefined(value) { | ||
} | ||
var isNotUndefined = bindNot(isUndefined); | ||
const isNotUndefined = bindNot(isUndefined); | ||
@@ -101,3 +89,3 @@ function isNullish(value) { | ||
} | ||
var isNotNullish = bindNot(isNullish); | ||
const isNotNullish = bindNot(isNullish); | ||
@@ -112,8 +100,4 @@ function asArray(possibleArg) { | ||
function optionalFunctionValue(value) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return isFunction(value) ? value.apply(void 0, args) : value; | ||
function optionalFunctionValue(value, ...args) { | ||
return isFunction(value) ? value(...args) : value; | ||
} | ||
@@ -132,6 +116,6 @@ | ||
} | ||
var isNotArray = bindNot(isArray); | ||
const isNotArray = bindNot(isArray); | ||
function last(values) { | ||
var valuesArray = asArray(values); | ||
const valuesArray = asArray(values); | ||
return valuesArray[valuesArray.length - 1]; | ||
@@ -144,5 +128,4 @@ } | ||
function transform(array, cb) { | ||
var res = []; | ||
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) { | ||
var v = array_1[_i]; | ||
const res = []; | ||
for (const v of array) { | ||
if (isArray(v)) { | ||
@@ -152,3 +135,3 @@ res.push(transform(v, cb)); | ||
else { | ||
var output = cb(v); | ||
const output = cb(v); | ||
if (isNotNull(output)) { | ||
@@ -165,3 +148,3 @@ res.push(output); | ||
function setValueAtPath(array, path, value) { | ||
var current = getCurrent(array, path); | ||
const current = getCurrent(array, path); | ||
current[last(path)] = value; | ||
@@ -171,3 +154,3 @@ return array; | ||
function flatten(values) { | ||
return asArray(values).reduce(function (acc, value) { | ||
return asArray(values).reduce((acc, value) => { | ||
if (isArray(value)) { | ||
@@ -180,5 +163,4 @@ return acc.concat(flatten(value)); | ||
function getCurrent(array, path) { | ||
var current = array; | ||
for (var _i = 0, _a = path.slice(0, -1); _i < _a.length; _i++) { | ||
var p = _a[_i]; | ||
let current = array; | ||
for (const p of path.slice(0, -1)) { | ||
current[p] = defaultTo(current[p], []); | ||
@@ -200,3 +182,3 @@ current = current[p]; | ||
function callEach(arr) { | ||
return arr.forEach(function (fn) { return fn(); }); | ||
return arr.forEach(fn => fn()); | ||
} | ||
@@ -248,3 +230,3 @@ | ||
function deferThrow(message) { | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
throw new Error(message); | ||
@@ -255,17 +237,17 @@ }, 0); | ||
function createBus() { | ||
var listeners = {}; | ||
const listeners = {}; | ||
return { | ||
emit: function (event, data) { | ||
listener(event).forEach(function (handler) { | ||
emit(event, data) { | ||
listener(event).forEach(handler => { | ||
handler(data); | ||
}); | ||
}, | ||
on: function (event, handler) { | ||
on(event, handler) { | ||
listeners[event] = listener(event).concat(handler); | ||
return { | ||
off: function () { | ||
listeners[event] = listener(event).filter(function (h) { return h !== handler; }); | ||
} | ||
off() { | ||
listeners[event] = listener(event).filter(h => h !== handler); | ||
}, | ||
}; | ||
} | ||
}, | ||
}; | ||
@@ -285,10 +267,11 @@ function listener(event) { | ||
*/ | ||
var seq = (function (n) { return function () { | ||
return "".concat(n++); | ||
}; })(0); | ||
const seq = genSeq(); | ||
function genSeq(namespace) { | ||
return ((n) => () => `${namespace ? namespace + '_' : ''}${n++}`)(0); | ||
} | ||
function mapFirst(array, callback) { | ||
var broke = false; | ||
var breakoutValue = null; | ||
for (var i = 0; i < array.length; i++) { | ||
let broke = false; | ||
let breakoutValue = null; | ||
for (let i = 0; i < array.length; i++) { | ||
callback(array[i], breakout, i); | ||
@@ -319,3 +302,3 @@ if (broke) { | ||
} | ||
var isNotEmpty = bindNot(isEmpty); | ||
const isNotEmpty = bindNot(isEmpty); | ||
@@ -326,2 +309,19 @@ function isPositive(value) { | ||
function createTinyState(initialValue) { | ||
let value; | ||
resetValue(); | ||
return () => [value, setValue, resetValue]; | ||
function setValue(nextValue) { | ||
value = optionalFunctionValue(nextValue, value); | ||
} | ||
function resetValue() { | ||
setValue(optionalFunctionValue(initialValue)); | ||
} | ||
} | ||
var tinyState = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
createTinyState: createTinyState | ||
}); | ||
exports.StringObject = StringObject; | ||
@@ -337,2 +337,3 @@ exports.asArray = asArray; | ||
exports.either = either; | ||
exports.genSeq = genSeq; | ||
exports.greaterThan = greaterThan; | ||
@@ -368,2 +369,3 @@ exports.hasOwnProperty = hasOwnProperty; | ||
exports.seq = seq; | ||
exports.tinyState = tinyState; | ||
@@ -370,0 +372,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,1 +0,1 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self)["vest-utils"]={})}(this,(function(n){"use strict";function t(n){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return!n.apply(void 0,t)}}function r(n){var t=String(n),r=Number(n),e=!isNaN(parseFloat(t))&&!isNaN(Number(n))&&isFinite(r);return Boolean(e)}var e=t(r);function u(n,t){return r(n)&&r(t)&&Number(n)===Number(t)}var i=t(u);function o(n,t){return u(n.length,t)}var f=t(o);function c(n,t){return r(n)&&r(t)&&Number(n)>Number(t)}function a(n,t){return c(n.length,t)}function l(n){return null===n}var s=t(l);function v(n){return void 0===n}var h=t(v);function d(n){return l(n)||v(n)}var g=t(d);function p(n){return[].concat(n)}function N(n){return"function"==typeof n}function y(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return N(n)?n.apply(void 0,t):n}function b(n,t){var r;return null!==(r=y(n))&&void 0!==r?r:y(t)}function m(n){return Boolean(Array.isArray(n))}var E=t(m);function O(n){var t=p(n);return t[t.length-1]}function _(n,t){for(var r=n,e=0,u=t.slice(0,-1);e<u.length;e++){var i=u[e];r[i]=b(r[i],[]),r=r[i]}return r}var j=Object.freeze({__proto__:null,transform:function n(t,r){for(var e=[],u=0,i=t;u<i.length;u++){var o=i[u];if(m(o))e.push(n(o,r));else{var f=r(o);s(f)&&e.push(f)}}return e},valueAtPath:function(n,t){return _(n,t)[O(t)]},setValueAtPath:function(n,t,r){return _(n,t)[O(t)]=r,n},flatten:function n(t){return p(t).reduce((function(t,r){return m(r)?t.concat(n(r)):p(t).concat(r)}),[])},getCurrent:_});function w(n,t){return Object.prototype.hasOwnProperty.call(n,t)}var A=Object.assign;var P,T=Object.freeze({__proto__:null,createBus:function(){var n={};return{emit:function(n,r){t(n).forEach((function(n){n(r)}))},on:function(r,e){return n[r]=t(r).concat(e),{off:function(){n[r]=t(r).filter((function(n){return n!==e}))}}}};function t(t){return n[t]||[]}}}),S=(P=0,function(){return"".concat(P++)});function q(n){return!n||(w(n,"length")?o(n,0):"object"==typeof n&&o(Object.keys(n),0))}var F=t(q);n.StringObject=function(n){return new String(y(n))},n.asArray=p,n.assign=A,n.bindNot=t,n.bus=T,n.cache=function(n){void 0===n&&(n=1);var t=[],r=function(e,u){var i=r.get(e);if(i)return i[1];var o=u();return t.unshift([e.concat(),o]),a(t,n)&&(t.length=n),o};return r.invalidate=function(n){var r=e(n);r>-1&&t.splice(r,1)},r.get=function(n){return t[e(n)]||null},r;function e(n){return t.findIndex((function(t){var r=t[0];return o(n,r.length)&&n.every((function(n,t){return n===r[t]}))}))}},n.callEach=function(n){return n.forEach((function(n){return n()}))},n.defaultTo=b,n.deferThrow=function(n){setTimeout((function(){throw new Error(n)}),0)},n.either=function(n,t){return!!n!=!!t},n.greaterThan=c,n.hasOwnProperty=w,n.invariant=function(n,t){if(!n)throw t instanceof String?t.valueOf():new Error(t?y(t):t)},n.isArray=m,n.isBoolean=function(n){return!!n===n},n.isEmpty=q,n.isFunction=N,n.isNotArray=E,n.isNotEmpty=F,n.isNotNull=s,n.isNotNullish=g,n.isNotNumeric=e,n.isNotUndefined=h,n.isNull=l,n.isNullish=d,n.isNumeric=r,n.isPositive=function(n){return c(n,0)},n.isPromise=function(n){return n&&N(n.then)},n.isStringValue=function(n){return String(n)===n},n.isUndefined=v,n.last=O,n.lengthEquals=o,n.lengthNotEquals=f,n.longerThan=a,n.mapFirst=function(n,t){for(var r=!1,e=null,u=0;u<n.length;u++)if(t(n[u],i,u),r)return e;function i(n,t){n&&(r=!0,e=t)}},n.nestedArray=j,n.numberEquals=u,n.numberNotEquals=i,n.optionalFunctionValue=y,n.seq=S,Object.defineProperty(n,"__esModule",{value:!0})})); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self)["vest-utils"]={})}(this,(function(n){"use strict";function t(n){return(...t)=>!n(...t)}function e(n){const t=String(n),e=Number(n),r=!isNaN(parseFloat(t))&&!isNaN(Number(n))&&isFinite(e);return Boolean(r)}const r=t(e);function o(n,t){return e(n)&&e(t)&&Number(n)===Number(t)}const u=t(o);function i(n,t){return o(n.length,t)}const c=t(i);function s(n,t){return e(n)&&e(t)&&Number(n)>Number(t)}function f(n,t){return s(n.length,t)}function l(n){return null===n}const a=t(l);function h(n){return void 0===n}const d=t(h);function g(n){return l(n)||h(n)}const p=t(g);function N(n){return[].concat(n)}function b(n){return"function"==typeof n}function y(n,...t){return b(n)?n(...t):n}function m(n,t){var e;return null!==(e=y(n))&&void 0!==e?e:y(t)}function v(n){return Boolean(Array.isArray(n))}const _=t(v);function E(n){const t=N(n);return t[t.length-1]}function O(n,t){let e=n;for(const n of t.slice(0,-1))e[n]=m(e[n],[]),e=e[n];return e}var j=Object.freeze({__proto__:null,transform:function n(t,e){const r=[];for(const o of t)if(v(o))r.push(n(o,e));else{const n=e(o);a(n)&&r.push(n)}return r},valueAtPath:function(n,t){return O(n,t)[E(t)]},setValueAtPath:function(n,t,e){return O(n,t)[E(t)]=e,n},flatten:function n(t){return N(t).reduce(((t,e)=>v(e)?t.concat(n(e)):N(t).concat(e)),[])},getCurrent:O});function S(n,t){return Object.prototype.hasOwnProperty.call(n,t)}var w=Object.assign;var A=Object.freeze({__proto__:null,createBus:function(){const n={};return{emit(n,e){t(n).forEach((n=>{n(e)}))},on:(e,r)=>(n[e]=t(e).concat(r),{off(){n[e]=t(e).filter((n=>n!==r))}})};function t(t){return n[t]||[]}}});const T=P();function P(n){return t=0,()=>`${n?n+"_":""}${t++}`;var t}function q(n){return!n||(S(n,"length")?i(n,0):"object"==typeof n&&i(Object.keys(n),0))}const F=t(q);var x=Object.freeze({__proto__:null,createTinyState:function(n){let t;return r(),()=>[t,e,r];function e(n){t=y(n,t)}function r(){e(y(n))}}});n.StringObject=function(n){return new String(y(n))},n.asArray=N,n.assign=w,n.bindNot=t,n.bus=A,n.cache=function(n=1){const t=[],e=(r,o)=>{const u=e.get(r);if(u)return u[1];const i=o();return t.unshift([r.concat(),i]),f(t,n)&&(t.length=n),i};return e.invalidate=n=>{const e=r(n);e>-1&&t.splice(e,1)},e.get=n=>t[r(n)]||null,e;function r(n){return t.findIndex((([t])=>i(n,t.length)&&n.every(((n,e)=>n===t[e]))))}},n.callEach=function(n){return n.forEach((n=>n()))},n.defaultTo=m,n.deferThrow=function(n){setTimeout((()=>{throw new Error(n)}),0)},n.either=function(n,t){return!!n!=!!t},n.genSeq=P,n.greaterThan=s,n.hasOwnProperty=S,n.invariant=function(n,t){if(!n)throw t instanceof String?t.valueOf():new Error(t?y(t):t)},n.isArray=v,n.isBoolean=function(n){return!!n===n},n.isEmpty=q,n.isFunction=b,n.isNotArray=_,n.isNotEmpty=F,n.isNotNull=a,n.isNotNullish=p,n.isNotNumeric=r,n.isNotUndefined=d,n.isNull=l,n.isNullish=g,n.isNumeric=e,n.isPositive=function(n){return s(n,0)},n.isPromise=function(n){return n&&b(n.then)},n.isStringValue=function(n){return String(n)===n},n.isUndefined=h,n.last=E,n.lengthEquals=i,n.lengthNotEquals=c,n.longerThan=f,n.mapFirst=function(n,t){let e=!1,r=null;for(let u=0;u<n.length;u++)if(t(n[u],o,u),e)return r;function o(n,t){n&&(e=!0,r=t)}},n.nestedArray=j,n.numberEquals=o,n.numberNotEquals=u,n.optionalFunctionValue=y,n.seq=T,n.tinyState=x,Object.defineProperty(n,"__esModule",{value:!0})})); |
{ | ||
"version": "0.0.5", | ||
"version": "0.1.0-dev-405df5", | ||
"name": "vest-utils", | ||
@@ -4,0 +4,0 @@ "author": "ealush", |
@@ -98,4 +98,4 @@ import _ from 'lodash'; | ||
const res = c([1, 2, 3], Math.random); | ||
expect(c.get([1, 2, 3])[0]).toEqual([1, 2, 3]); | ||
expect(c.get([1, 2, 3])[1]).toEqual(res); | ||
expect(c.get([1, 2, 3])?.[0]).toEqual([1, 2, 3]); | ||
expect(c.get([1, 2, 3])?.[1]).toEqual(res); | ||
}); | ||
@@ -102,0 +102,0 @@ }); |
@@ -1,9 +0,11 @@ | ||
import { random, datatype } from 'faker'; | ||
import { faker } from '@faker-js/faker'; | ||
import { greaterThan } from 'greaterThan'; | ||
const { random, datatype } = faker; | ||
describe('Tests greaterThan rule', () => { | ||
let arg0; | ||
describe('Arguments are numbers', () => { | ||
let arg0: number; | ||
describe('Arguments are numbers', () => { | ||
beforeEach(() => { | ||
@@ -33,5 +35,11 @@ arg0 = datatype.number(); | ||
describe('Arguments are numeric strings', () => { | ||
let arg0: string; | ||
beforeEach(() => { | ||
arg0 = datatype.number().toString(); | ||
}); | ||
describe('When first argument is larger', () => { | ||
it('Should return true', () => { | ||
expect(greaterThan(`${arg0}`, `${arg0 - 1}`)).toBe(true); | ||
expect(greaterThan('100', '99')).toBe(true); | ||
}); | ||
@@ -56,2 +64,3 @@ }); | ||
it('Should return false', () => { | ||
// @ts-expect-error - testing invalid input | ||
expect(greaterThan(element, 0)).toBe(false); | ||
@@ -58,0 +67,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import faker from 'faker'; | ||
import { faker } from '@faker-js/faker'; | ||
@@ -49,6 +49,8 @@ import { lengthEquals } from 'lengthEquals'; | ||
it('Should return false for number argument', () => { | ||
expect(lengthEquals(length, 0)).toBe(false); | ||
// @ts-expect-error - testing wrong input | ||
expect(lengthEquals(100, 0)).toBe(false); | ||
}); | ||
it('Should return false for boolean argument', () => { | ||
// @ts-expect-error - testing wrong input | ||
expect(lengthEquals(boolean, 0)).toBe(false); | ||
@@ -55,0 +57,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import faker from 'faker'; | ||
import { faker } from '@faker-js/faker'; | ||
@@ -49,2 +49,3 @@ import { longerThan } from 'longerThan'; | ||
it('Should return false for number argument', () => { | ||
// @ts-expect-error - testing wrong input | ||
expect(longerThan(length, 0)).toBe(false); | ||
@@ -54,2 +55,3 @@ }); | ||
it('Should return false for boolean argument', () => { | ||
// @ts-expect-error - testing wrong input | ||
expect(longerThan(boolean, 0)).toBe(false); | ||
@@ -56,0 +58,0 @@ }); |
@@ -1,9 +0,10 @@ | ||
import { random, datatype } from 'faker'; | ||
import { faker } from '@faker-js/faker'; | ||
import { numberEquals } from 'numberEquals'; | ||
const { random, datatype } = faker; | ||
describe('Tests numberEquals rule', () => { | ||
let arg0; | ||
describe('Arguments are numbers', () => { | ||
let arg0: number; | ||
beforeEach(() => { | ||
@@ -33,5 +34,11 @@ arg0 = datatype.number(); | ||
describe('Arguments are numeric strings', () => { | ||
let arg0: string; | ||
beforeEach(() => { | ||
arg0 = datatype.number().toString(); | ||
}); | ||
describe('When first argument is larger', () => { | ||
it('Should return false', () => { | ||
expect(numberEquals(`${arg0}`, `${arg0 - 1}`)).toBe(false); | ||
expect(numberEquals(`${arg0}`, `${Number(arg0) - 1}`)).toBe(false); | ||
}); | ||
@@ -48,3 +55,3 @@ }); | ||
it('Should return true', () => { | ||
expect(numberEquals(arg0, arg0)).toBe(true); | ||
expect(numberEquals('100', '100')).toBe(true); | ||
}); | ||
@@ -57,2 +64,3 @@ }); | ||
it('Should return false', () => { | ||
// @ts-expect-error - testing invalid input | ||
expect(numberEquals(element, 0)).toBe(false); | ||
@@ -59,0 +67,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import seq from 'seq'; | ||
import seq, { genSeq } from 'seq'; | ||
@@ -12,2 +12,18 @@ describe('lib:seq', () => { | ||
}); | ||
describe('genSeq', () => { | ||
it('Creates a namespaced sequence', () => { | ||
const seq = genSeq('test'); | ||
expect(seq()).toBe('test_0'); | ||
expect(seq()).toBe('test_1'); | ||
expect(seq()).toBe('test_2'); | ||
const seq2 = genSeq('test2'); | ||
expect(seq2()).toBe('test2_0'); | ||
expect(seq2()).toBe('test2_1'); | ||
expect(seq2()).toBe('test2_2'); | ||
expect(seq()).toBe('test_3'); | ||
}); | ||
}); | ||
}); |
import type { CB } from 'utilityTypes'; | ||
export function createBus(): { | ||
on: (event: string, handler: CB) => OnReturn; | ||
emit: (event: string, ...args: any[]) => void; | ||
} { | ||
export function createBus(): BusType { | ||
const listeners: Record<string, CB[]> = {}; | ||
@@ -33,1 +30,6 @@ | ||
type OnReturn = { off: () => void }; | ||
export type BusType = { | ||
on: (event: string, handler: CB) => OnReturn; | ||
emit: (event: string, ...args: any[]) => void; | ||
}; |
@@ -7,10 +7,12 @@ import { lengthEquals } from 'lengthEquals'; | ||
*/ | ||
export default function createCache(maxSize = 1): { | ||
<T>(deps: unknown[], cacheAction: (...args: unknown[]) => T): T; | ||
get(deps: unknown[]): any; | ||
export default function createCache<T = unknown>( | ||
maxSize = 1 | ||
): { | ||
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T; | ||
get(deps: unknown[]): [unknown[], T] | null; | ||
invalidate(item: any): void; | ||
} { | ||
const cacheStorage: Array<[unknown[], any]> = []; | ||
const cacheStorage: Array<[unknown[], T]> = []; | ||
const cache = <T>( | ||
const cache = ( | ||
deps: unknown[], | ||
@@ -38,3 +40,3 @@ cacheAction: (...args: unknown[]) => T | ||
// Retrieves an item from the cache. | ||
cache.get = (deps: unknown[]): [unknown[], any] | null => | ||
cache.get = (deps: unknown[]): [unknown[], T] | null => | ||
cacheStorage[findIndex(deps)] || null; | ||
@@ -52,1 +54,7 @@ | ||
} | ||
export type CacheApi<T = unknown> = { | ||
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T; | ||
get(deps: unknown[]): [unknown[], T] | null; | ||
invalidate(item: any): void; | ||
}; |
@@ -6,1 +6,3 @@ export default function deferThrow(message?: string): void { | ||
} | ||
export type TDeferThrow = typeof deferThrow; |
@@ -5,7 +5,11 @@ /** | ||
const seq: () => string = ( | ||
(n: number) => (): string => | ||
`${n++}` | ||
)(0); | ||
const seq = genSeq(); | ||
export default seq; | ||
export function genSeq(namespace?: string): () => string { | ||
return ( | ||
(n: number) => () => | ||
`${namespace ? namespace + '_' : ''}${n++}` | ||
)(0); | ||
} |
@@ -1,2 +0,4 @@ | ||
export { default as cache } from 'cache'; | ||
export { default as cache, CacheApi } from 'cache'; | ||
export { BusType } from 'bus'; | ||
export { TinyState } from 'tinyState'; | ||
export { isNullish, isNotNullish } from 'isNullish'; | ||
@@ -19,3 +21,3 @@ export * as nestedArray from 'nestedArray'; | ||
export * as bus from 'bus'; | ||
export { default as seq } from 'seq'; | ||
export { default as seq, genSeq } from 'seq'; | ||
export { default as isFunction } from 'isFunction'; | ||
@@ -33,2 +35,3 @@ export { default as mapFirst } from 'mapFirst'; | ||
export { isPositive } from 'isPositive'; | ||
export * as tinyState from 'tinyState'; | ||
@@ -35,0 +38,0 @@ export type { DropFirst } from 'utilityTypes'; |
@@ -42,2 +42,3 @@ { | ||
"seq": ["src/seq.ts"], | ||
"tinyState": ["src/tinyState.ts"], | ||
"utilityTypes": ["src/utilityTypes.ts"], | ||
@@ -44,0 +45,0 @@ "vest-utils": ["src/vest-utils.ts"] |
/** | ||
* Creates a cache function | ||
*/ | ||
declare function createCache(maxSize?: number): { | ||
<T>(deps: unknown[], cacheAction: (...args: unknown[]) => T): T; | ||
get(deps: unknown[]): any; | ||
declare function createCache<T = unknown>(maxSize?: number): { | ||
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T; | ||
get(deps: unknown[]): [ | ||
unknown[], | ||
T | ||
] | null; | ||
invalidate(item: any): void; | ||
}; | ||
type CacheApi<T = unknown> = { | ||
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T; | ||
get(deps: unknown[]): [ | ||
unknown[], | ||
T | ||
] | null; | ||
invalidate(item: any): void; | ||
}; | ||
type DropFirst<T extends unknown[]> = T extends [ | ||
unknown, | ||
...infer U | ||
] ? U : never; | ||
type Stringable = string | ((...args: any[]) => string); | ||
type CB = (...args: any[]) => any; | ||
type ValueOf<T> = T[keyof T]; | ||
type OnReturn = { | ||
off: () => void; | ||
}; | ||
type BusType = { | ||
on: (event: string, handler: CB) => OnReturn; | ||
emit: (event: string, ...args: any[]) => void; | ||
}; | ||
type TinyState<S> = () => [ | ||
value: S, | ||
setValue: (next: S | ((prev: S) => S)) => void, | ||
resetValue: () => void | ||
]; | ||
declare function isNullish(value: any): value is null | undefined; | ||
@@ -23,9 +53,2 @@ declare const isNotNullish: (value: any) => boolean; | ||
declare function asArray<T>(possibleArg: T | T[]): T[]; | ||
type DropFirst<T extends unknown[]> = T extends [ | ||
unknown, | ||
...infer U | ||
] ? U : never; | ||
type Stringable = string | ((...args: any[]) => string); | ||
type CB = (...args: any[]) => any; | ||
type ValueOf<T> = T[keyof T]; | ||
declare function callEach(arr: CB[]): void; | ||
@@ -64,9 +87,10 @@ /** | ||
type ValueOf<T> = T[keyof T]; | ||
function createBus(): { | ||
function createBus(): BusType; | ||
type OnReturn = { | ||
off: () => void; | ||
}; | ||
type BusType = { | ||
on: (event: string, handler: CB) => OnReturn; | ||
emit: (event: string, ...args: any[]) => void; | ||
}; | ||
type OnReturn = { | ||
off: () => void; | ||
}; | ||
} | ||
@@ -77,2 +101,3 @@ /** | ||
declare const seq: () => string; | ||
declare function genSeq(namespace?: string): () => string; | ||
declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown; | ||
@@ -100,4 +125,12 @@ declare function mapFirst<T>(array: T[], callback: (item: T, breakout: (conditional: boolean, value: unknown) => void, index: number) => unknown): any; | ||
declare function isPositive(value: number | string): boolean; | ||
export { createCache as cache, isNullish, isNotNullish, nestedArray, asArray, callEach, hasOwnProperty, isPromise, optionalFunctionValue, _default as assign, defaultTo, invariant, StringObject, isStringValue, bindNot, either, isBoolean, last, deferThrow, bus, seq, isFunction, mapFirst, greaterThan, longerThan, isNumeric, isNotNumeric, lengthEquals, lengthNotEquals, numberEquals, numberNotEquals, isNull, isNotNull, isUndefined, isNotUndefined, isArray, isNotArray, isEmpty, isNotEmpty, isPositive }; | ||
declare namespace tinyState { | ||
function createTinyState<S>(initialValue: S | (() => S)): TinyState<S>; | ||
type TinyState<S> = () => [ | ||
value: S, | ||
setValue: (next: S | ((prev: S) => S)) => void, | ||
resetValue: () => void | ||
]; | ||
} | ||
export { createCache as cache, CacheApi, BusType, TinyState, isNullish, isNotNullish, nestedArray, asArray, callEach, hasOwnProperty, isPromise, optionalFunctionValue, _default as assign, defaultTo, invariant, StringObject, isStringValue, bindNot, either, isBoolean, last, deferThrow, bus, seq, genSeq, isFunction, mapFirst, greaterThan, longerThan, isNumeric, isNotNumeric, lengthEquals, lengthNotEquals, numberEquals, numberNotEquals, isNull, isNotNull, isUndefined, isNotUndefined, isArray, isNotArray, isEmpty, isNotEmpty, isPositive, tinyState }; | ||
export type { DropFirst, Stringable, CB, ValueOf }; | ||
//# sourceMappingURL=vest-utils.d.ts.map |
Sorry, the diff of this file is not supported yet
86411
68
2225