Comparing version 15.1.0 to 15.2.0
# Changelog | ||
## 15.2.0 | ||
* add assignSelected | ||
* requires Object.hasOwn | ||
## 15.1.0 | ||
@@ -4,0 +9,0 @@ |
@@ -474,3 +474,3 @@ export { | ||
**/ | ||
if (!{}.hasOwnProperty.call(obj2, key)) { | ||
if (!Object.hasOwn(obj2, key)) { | ||
const nameArray = []; | ||
@@ -486,3 +486,3 @@ nameArray.push(key); | ||
Object.keys(obj2).forEach(key => { | ||
if (!{}.hasOwnProperty.call(obj1, key)) { | ||
if (!Object.hasOwn(obj1, key)) { | ||
const nameArray = []; | ||
@@ -489,0 +489,0 @@ nameArray.push(key); |
{ | ||
"name": "utilsac", | ||
"version": "15.1.0", | ||
"version": "15.2.0", | ||
"description": "Utility functions", | ||
@@ -16,4 +16,4 @@ "license": "CC0-1.0", | ||
"ava": "^3.15.0", | ||
"eslint": "^8.4.1", | ||
"eslint-config-red": "^1.8.2", | ||
"eslint": "^8.20.0", | ||
"eslint-config-red": "^1.9.1", | ||
"leistung": "^5.0.1" | ||
@@ -20,0 +20,0 @@ }, |
@@ -31,2 +31,3 @@ # [utilsac](https://github.com/GrosSacASac/utilsac) | ||
bytesLengthFromString, | ||
assignSelected, | ||
} from "utilsac"; | ||
@@ -33,0 +34,0 @@ |
115
utility.js
@@ -16,2 +16,3 @@ export { | ||
bytesLengthFromString, | ||
assignSelected, | ||
}; | ||
@@ -21,8 +22,8 @@ | ||
/** creates a function that is de-bounced, | ||
calling it, will eventually execute it, when you stop calling it | ||
useful for scroll events, resize, search etc | ||
the returned function always returns undefined */ | ||
const createDebounced = function (functionToDebounce, waitTime = timeDefault) { | ||
/* creates a function that is de-bounced, | ||
calling it, will eventually execute it, when you stop calling it | ||
useful for scroll events, resize, search etc | ||
the returned function always returns undefined */ | ||
let timeOutId = 0; | ||
@@ -41,8 +42,8 @@ return function (...args) { | ||
/** creates a function that is throttled, | ||
calling it once will execute it immediately | ||
calling it very often during a period less than minimumTimeSpace will only execute it once | ||
the returned function always returns undefined */ | ||
const createThrottled = function (functionToThrottle, minimumTimeSpace = timeDefault) { | ||
/* creates a function that is throttled, | ||
calling it once will execute it immediately | ||
calling it very often during a period less than minimumTimeSpace will only execute it once | ||
the returned function always returns undefined */ | ||
let lastTime = Number.MIN_SAFE_INTEGER; | ||
@@ -59,11 +60,10 @@ return function (...args) { | ||
/** creates a function that is throttled, | ||
calling it once will execute it immediately | ||
calling it very often during a period less than minimumTimeSpace will only execute it twice: | ||
the first and last call | ||
The last call is always eventually executed | ||
the returned function always returns undefined */ | ||
const throttledWithLast = function (functionToThrottle, minimumTimeSpace = timeDefault) { | ||
/* creates a function that is throttled, | ||
calling it once will execute it immediately | ||
calling it very often during a period less than minimumTimeSpace will only execute it twice: | ||
the first and last call | ||
The last call is always eventually executed | ||
the returned function always returns undefined */ | ||
let timeOutId = 0; | ||
@@ -97,6 +97,6 @@ let lastTime = Number.MIN_SAFE_INTEGER; | ||
/** different than Promise.all, takes an array of functions that return a promise or value | ||
only executes promiseCreators sequentially | ||
resolves with an array of values or reject with the first error*/ | ||
const chainPromises = function (promiseCreators) { | ||
/* different than Promise.all, takes an array of functions that return a promise or value | ||
only executes promiseCreators sequentially | ||
resolves with an array of values or reject with the first error*/ | ||
const {length} = promiseCreators; | ||
@@ -121,9 +121,8 @@ const values = []; | ||
/** forces a function that returns a promise to be sequential | ||
useful for fs for example */ | ||
const decorateForceSequential = function (promiseCreator) { | ||
/* forces a function that returns a promise to be sequential | ||
useful for fs for example */ | ||
let lastPromise = Promise.resolve(); | ||
return async function (...x) { | ||
const promiseWeAreWaitingFor = lastPromise; | ||
let currentPromise; | ||
let callback; | ||
@@ -136,3 +135,3 @@ // we need to change lastPromise before await anything, | ||
await promiseWeAreWaitingFor; | ||
currentPromise = promiseCreator(...x); | ||
const currentPromise = promiseCreator(...x); | ||
currentPromise.then(callback).catch(callback); | ||
@@ -143,6 +142,7 @@ return currentPromise; | ||
/** same as chainPromises except it will run up to x amount of | ||
promise in parallel | ||
resolves with an array of values or reject with the first error **/ | ||
const somePromisesParallel = function (promiseCreators, x = 10) { | ||
const somePromisesParallel = function (promiseCreators, x = 10) { | ||
/* same as chainPromises except it will run up to x amount of | ||
promise in parallel */ | ||
const {length} = promiseCreators; | ||
@@ -184,7 +184,7 @@ const values = []; | ||
/** different than Promise.all | ||
only executes promiseCreator one after the previous has resolved | ||
useful for testing | ||
resolves with an array of values */ | ||
const chainPromiseNTimes = function (promiseCreator, times) { | ||
/* different than Promise.all | ||
only executes promiseCreator one after the previous has resolved | ||
useful for testing | ||
resolves with an array of values */ | ||
return chainPromises(Array.from({length: times}).fill(promiseCreator)); | ||
@@ -216,4 +216,4 @@ }; | ||
/** executes callback and returns time elapsed in ms */ | ||
const timeFunction = function (callback, timer = Date) { | ||
// executes callback and returns time elapsed in ms | ||
const startTime = timer.now(); | ||
@@ -225,6 +225,6 @@ callback(); | ||
/** returns a Promise that resolves with | ||
the time elapsed for the promise to resolve and its value | ||
executes promiseCreator and waits for it to resolve */ | ||
const timePromise = function (promiseCreator, timer = Date) { | ||
/* returns a Promise that resolves with | ||
the time elapsed for the promise to resolve and its value | ||
executes promiseCreator and waits for it to resolve */ | ||
const startTime = timer.now(); | ||
@@ -241,8 +241,7 @@ return promiseCreator().then(function (value) { | ||
/** joins together the args as strings to | ||
decide if arguments are the same | ||
fast memoizer | ||
but infinitely growing */ | ||
const memoizeAsStrings = function (functionToMemoize, separator = `-`) { | ||
/* joins together the args as strings to | ||
decide if arguments are the same | ||
fast memoizer | ||
but infinitely growing */ | ||
const previousResults = new Map(); | ||
@@ -263,11 +262,10 @@ return function (...args) { | ||
/** creates a template tag function | ||
that will map the provided function on all runtime values | ||
before constructing the string | ||
example: | ||
const createURLString = createTemplateTag(encodeURIComponent) | ||
createURLString`https://example.com/id/${`slashes and spaces are properly escaped ///`}`; | ||
// -> "https://example.com/id/slashes%20and%20spaces%20are%20properly%20escaped%20%2F%2F%2F" */ | ||
const createTemplateTag = (mapper) => { | ||
/* creates a template tag function | ||
that will map the provided function on all runtime values | ||
before constructing the string | ||
example: | ||
const createURLString = createTemplateTag(encodeURIComponent) | ||
createURLString`https://example.com/id/${`slashes and spaces are properly escaped ///`}`; | ||
// -> "https://example.com/id/slashes%20and%20spaces%20are%20properly%20escaped%20%2F%2F%2F" */ | ||
return (staticStrings, ...parts) => { | ||
@@ -284,1 +282,20 @@ return Array.from(parts, (part, index) => { | ||
}; | ||
/** Similar to Object.assign, except it takes a white list as first argument */ | ||
const assignSelected = (list, target, ...sources) => { | ||
sources.forEach(source => { | ||
if (!source || typeof source !== `object`) { | ||
return; | ||
} | ||
Object.entries(source).forEach(([key, value]) => { | ||
if (key === `__proto__`) { | ||
return; | ||
} | ||
if (!list.includes(key)) { | ||
return; | ||
} | ||
target[key] = value; | ||
}); | ||
}); | ||
return target; | ||
}; |
40468
824
162