@react-pdf/fns
Advanced tools
Comparing version
331
lib/index.js
/** | ||
* Applies a function to the value at the given index of an array | ||
* @param {number} index | ||
* @param {function} fn | ||
* @param {array} collection | ||
* @returns copy of the array with the element at the given index replaced with the result of the function application. | ||
* | ||
* @param index | ||
* @param fn | ||
* @param collection | ||
* @returns Copy of the array with the element at the given index replaced with the result of the function application. | ||
*/ | ||
const adjust = (index, fn, collection) => { | ||
if (index >= 0 && index >= collection.length) return collection; | ||
if (index < 0 && Math.abs(index) > collection.length) return collection; | ||
const i = index < 0 ? collection.length + index : index; | ||
return Object.assign([], collection, { | ||
[i]: fn(collection[i]) | ||
}); | ||
if (index >= 0 && index >= collection.length) | ||
return collection; | ||
if (index < 0 && Math.abs(index) > collection.length) | ||
return collection; | ||
const i = index < 0 ? collection.length + index : index; | ||
return Object.assign([], collection, { [i]: fn(collection[i]) }); | ||
}; | ||
@@ -21,40 +21,23 @@ | ||
* | ||
* @template {unknown} T | ||
* @param {T[]} list list to be reversed | ||
* @returns {T[]} reversed list | ||
* @template T | ||
* @param list - List to be reversed | ||
* @returns Reversed list | ||
*/ | ||
const reverse = list => Array.prototype.slice.call(list, 0).reverse(); | ||
const reverse = (list) => Array.prototype.slice.call(list, 0).reverse(); | ||
/* eslint-disable no-await-in-loop */ | ||
/** | ||
* @typedef {Function} AsyncCompose | ||
* @param {any} value | ||
* @param {...any} args | ||
* @returns {any} result | ||
*/ | ||
/** | ||
* Performs right-to-left function composition with async functions support | ||
* | ||
* @param {...Function} fns functions | ||
* @returns {AsyncCompose} composed function | ||
* @param fns - Functions | ||
* @returns Composed function | ||
*/ | ||
const asyncCompose = function () { | ||
for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) { | ||
fns[_key] = arguments[_key]; | ||
} | ||
return async function (value) { | ||
const asyncCompose = (...fns) => async (value, ...args) => { | ||
let result = value; | ||
const reversedFns = reverse(fns); | ||
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
for (let i = 0; i < reversedFns.length; i += 1) { | ||
const fn = reversedFns[i]; | ||
result = await fn(result, ...args); | ||
const fn = reversedFns[i]; | ||
result = await fn(result, ...args); | ||
} | ||
return result; | ||
}; | ||
}; | ||
@@ -65,8 +48,9 @@ | ||
* | ||
* @param {string} value string | ||
* @returns {string} capitalized string | ||
* @param value = Any string | ||
* @returns Capitalized string | ||
*/ | ||
const capitalize = value => { | ||
if (!value) return value; | ||
return value.replace(/(^|\s)\S/g, l => l.toUpperCase()); | ||
const capitalize = (value) => { | ||
if (!value) | ||
return value; | ||
return value.replace(/(^|\s)\S/g, (l) => l.toUpperCase()); | ||
}; | ||
@@ -77,42 +61,24 @@ | ||
* | ||
* @template T | ||
* @param {T|T[]} value value | ||
* @returns {T[]} array | ||
* @template T - The type of the value. | ||
* @param value - The value to cast into an array. | ||
* @returns An array containing the given value. | ||
*/ | ||
const castArray = value => { | ||
return Array.isArray(value) ? value : [value]; | ||
const castArray = (value) => { | ||
return Array.isArray(value) ? value : [value]; | ||
}; | ||
/* eslint-disable no-await-in-loop */ | ||
/** | ||
* @typedef {Function} Compose | ||
* @param {any} value | ||
* @param {...any} args | ||
* @returns {any} result | ||
*/ | ||
/** | ||
* Performs right-to-left function composition | ||
* | ||
* @param {...Function} fns functions | ||
* @returns {Compose} composed function | ||
* @param fns - Functions | ||
* @returns Composed function | ||
*/ | ||
const compose = function () { | ||
for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) { | ||
fns[_key] = arguments[_key]; | ||
} | ||
return function (value) { | ||
const compose = (...fns) => (value, ...args) => { | ||
let result = value; | ||
const reversedFns = reverse(fns); | ||
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
const reversedFns = fns.slice().reverse(); | ||
for (let i = 0; i < reversedFns.length; i += 1) { | ||
const fn = reversedFns[i]; | ||
result = fn(result, ...args); | ||
const fn = reversedFns[i]; | ||
result = fn(result, ...args); | ||
} | ||
return result; | ||
}; | ||
}; | ||
@@ -124,6 +90,6 @@ | ||
* @template T | ||
* @param {T[]} array the array to drop the last element from | ||
* @returns {T[]} the new array with the last element dropped | ||
* @param array - The array to drop the last element from | ||
* @returns - The new array with the last element dropped | ||
*/ | ||
const dropLast = array => array.slice(0, array.length - 1); | ||
const dropLast = (array) => array.slice(0, array.length - 1); | ||
@@ -134,22 +100,21 @@ /** | ||
* @template T | ||
* @param {Record<string, (value: T) => T | Record<string, (value: T) => T>>} transformations - The transformations to apply. | ||
* @param {T} object the object to transform. | ||
* @returns {T} the transformed object. | ||
* @param transformations - The transformations to apply. | ||
* @param object - The object to transform. | ||
* @returns The transformed object. | ||
*/ | ||
const evolve = (transformations, object) => { | ||
const result = object instanceof Array ? [] : {}; | ||
const keys = Object.keys(object); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
const transformation = transformations[key]; | ||
const type = typeof transformation; | ||
if (type === 'function') { | ||
result[key] = transformation(object[key]); | ||
} else if (transformation && type === 'object') { | ||
result[key] = evolve(transformation, object[key]); | ||
} else { | ||
result[key] = object[key]; | ||
const result = {}; | ||
const keys = Object.keys(object); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
const transformation = transformations[key]; | ||
const type = typeof transformation; | ||
if (type === 'function') { | ||
result[key] = transformation(object[key]); | ||
} | ||
else { | ||
result[key] = object[key]; | ||
} | ||
} | ||
} | ||
return result; | ||
return result; | ||
}; | ||
@@ -160,7 +125,7 @@ | ||
* | ||
* @template {unknown} T | ||
* @param {T} value the value to check | ||
* @returns {T is null | undefined} true if the value is null or undefined, false otherwise | ||
* @template T - The type of the value. | ||
* @param value - The value to check | ||
* @returns True if the value is null or undefined, false otherwise | ||
*/ | ||
const isNil = value => value === null || value === undefined; | ||
const isNil = (value) => value === null || value === undefined; | ||
@@ -170,27 +135,23 @@ /** | ||
* | ||
* @param {object} target the object to retrieve the value from. | ||
* @param {string | string[]} path the path of the value to retrieve. | ||
* @param {*} defaultValue the default value to return if the path does not exist. | ||
* @returns {*} the value at the given path, or the default value if the path does not exist. | ||
* @param target - The object to retrieve the value from. | ||
* @param path - The path of the value to retrieve. | ||
* @param defaultValue - The default value to return if the path does not exist. | ||
* @returns The value at the given path, or the default value if the path does not exist. | ||
*/ | ||
const get = (target, path, defaultValue) => { | ||
if (isNil(target)) return defaultValue; | ||
const _path = castArray(path); | ||
let result = target; | ||
for (let i = 0; i < _path.length; i += 1) { | ||
if (isNil(result)) return undefined; | ||
result = result[_path[i]]; | ||
} | ||
return isNil(result) ? defaultValue : result; | ||
if (isNil(target)) | ||
return defaultValue; | ||
const _path = castArray(path); | ||
let result = target; | ||
for (let i = 0; i < _path.length; i += 1) { | ||
if (isNil(result)) | ||
return undefined; | ||
result = result[_path[i]]; | ||
} | ||
return isNil(result) ? defaultValue : result; | ||
}; | ||
/** | ||
* Returns the last character of a string. | ||
* | ||
* @param {string} value the input string | ||
* @returns {string} the last character of the string | ||
*/ | ||
const last = value => { | ||
return value === '' ? '' : value[value.length - 1]; | ||
}; | ||
function last(value) { | ||
return value === '' ? '' : value[value.length - 1]; | ||
} | ||
@@ -200,38 +161,30 @@ /** | ||
* | ||
* @param {Object} object the object to map over | ||
* @param {Function} fn the function to apply to each value | ||
* @returns {Object} a new object with the mapped values | ||
* @param object - The object to map over | ||
* @param fn - The function to apply to each value | ||
* @returns A new object with the mapped values | ||
*/ | ||
const mapValues = (object, fn) => { | ||
const entries = Object.entries(object); | ||
return entries.reduce((acc, _ref, index) => { | ||
let [key, value] = _ref; | ||
acc[key] = fn(value, key, index); | ||
return acc; | ||
}, {}); | ||
const entries = Object.entries(object); | ||
const acc = {}; | ||
return entries.reduce((acc, [key, value], index) => { | ||
acc[key] = fn(value, key, index); | ||
return acc; | ||
}, acc); | ||
}; | ||
const isPercent = (value) => /((-)?\d+\.?\d*)%/g.exec(`${value}`); | ||
/** | ||
* @param {string | number} value | ||
* @returns {RegExpExecArray | null} match | ||
*/ | ||
const isPercent = value => /((-)?\d+\.?\d*)%/g.exec(`${value}`); | ||
/** | ||
* Get percentage value of input | ||
* | ||
* @param {string | number} value | ||
* @returns {{ percent: number, value: number } | null} percent value (if matches) | ||
* @param value | ||
* @returns Percent value (if matches) | ||
*/ | ||
const matchPercent = value => { | ||
const match = isPercent(value); | ||
if (match) { | ||
const f = parseFloat(match[1]); | ||
const percent = f / 100; | ||
return { | ||
percent, | ||
value: f | ||
}; | ||
} | ||
return null; | ||
const matchPercent = (value) => { | ||
const match = isPercent(value); | ||
if (match) { | ||
const f = parseFloat(match[1]); | ||
const percent = f / 100; | ||
return { percent, value: f }; | ||
} | ||
return null; | ||
}; | ||
@@ -242,13 +195,13 @@ | ||
* | ||
* @param {string|string[]} keys the key or keys to omit | ||
* @param {object} object the original object | ||
* @returns {object} the new object without the omitted keys | ||
* @param keys - The key or keys to omit | ||
* @param object - The original object | ||
* @returns The new object without the omitted keys | ||
*/ | ||
const omit = (keys, object) => { | ||
const _keys = castArray(keys); | ||
const copy = Object.assign({}, object); | ||
_keys.forEach(key => { | ||
delete copy[key]; | ||
}); | ||
return copy; | ||
const _keys = castArray(keys); | ||
const copy = Object.assign({}, object); | ||
_keys.forEach((key) => { | ||
delete copy[key]; | ||
}); | ||
return copy; | ||
}; | ||
@@ -259,13 +212,14 @@ | ||
* | ||
* @param {string[]} keys the keys to pick from the object | ||
* @param {object} obj the object to pick the keys from | ||
* @returns {object} a new object with only the picked keys | ||
* @param keys - The keys to pick from the object | ||
* @param object - The object to pick the keys from | ||
* @returns A new object with only the picked keys | ||
*/ | ||
const pick = (keys, obj) => { | ||
const result = {}; | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
if (key in obj) result[key] = obj[key]; | ||
} | ||
return result; | ||
const result = {}; | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
if (key in obj) | ||
result[key] = obj[key]; | ||
} | ||
return result; | ||
}; | ||
@@ -276,16 +230,13 @@ | ||
* | ||
* @template {unknown} T | ||
* @param {T} element element to be repeated | ||
* @param {number} length number of times to repeat element | ||
* @returns {T[]} repeated elements | ||
* @template T | ||
* @param element - Element to be repeated | ||
* @param length - Number of times to repeat element | ||
* @returns Repeated elements | ||
*/ | ||
const repeat = function (elem, length) { | ||
if (length === void 0) { | ||
length = 0; | ||
} | ||
const result = new Array(length); | ||
for (let i = 0; i < length; i += 1) { | ||
result[i] = elem; | ||
} | ||
return result; | ||
const repeat = (element, length = 0) => { | ||
const result = new Array(length); | ||
for (let i = 0; i < length; i += 1) { | ||
result[i] = element; | ||
} | ||
return result; | ||
}; | ||
@@ -296,8 +247,9 @@ | ||
* | ||
* @param {string} value string | ||
* @returns {string} capitalized string | ||
* @param value - String | ||
* @returns Capitalized string | ||
*/ | ||
const upperFirst = value => { | ||
if (!value) return value; | ||
return value.charAt(0).toUpperCase() + value.slice(1); | ||
const upperFirst = (value) => { | ||
if (!value) | ||
return value; | ||
return value.charAt(0).toUpperCase() + value.slice(1); | ||
}; | ||
@@ -308,15 +260,16 @@ | ||
* | ||
* @param {any[]} keys the keys to pick from the object | ||
* @param {any[]} array to filter the values from | ||
* @returns {any[]} a new array with without the omitted values | ||
* @param keys - The keys to pick from the object | ||
* @param array - Array to filter the values from | ||
* @returns A new array with without the omitted values | ||
*/ | ||
const without = (keys, array) => { | ||
const result = []; | ||
for (let i = 0; i < array.length; i += 1) { | ||
const value = array[i]; | ||
if (!keys.includes(value)) result.push(value); | ||
} | ||
return result; | ||
const result = []; | ||
for (let i = 0; i < array.length; i += 1) { | ||
const value = array[i]; | ||
if (!keys.includes(value)) | ||
result.push(value); | ||
} | ||
return result; | ||
}; | ||
export { adjust, asyncCompose, capitalize, castArray, compose, dropLast, evolve, get, isNil, last, mapValues, matchPercent, omit, pick, repeat, reverse, upperFirst, without }; |
{ | ||
"name": "@react-pdf/fns", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"license": "MIT", | ||
@@ -10,2 +10,3 @@ "description": "React-pdf helper functions", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"repository": { | ||
@@ -18,8 +19,5 @@ "type": "git", | ||
"test": "vitest", | ||
"build": "rimraf ./lib && rollup -c", | ||
"watch": "rimraf ./lib && rollup -c -w" | ||
"build": "rollup -c", | ||
"watch": "rollup -c -w" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.20.13" | ||
}, | ||
"files": [ | ||
@@ -26,0 +24,0 @@ "lib" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13586
42.92%0
-100%4
33.33%403
42.4%1
Infinity%- Removed
- Removed
- Removed