Installations
npm install @sandstreamdev/std
Developer Guide
Typescript
Yes
Module System
ESM, UMD
Node Version
20.15.0
NPM Version
10.7.0
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (73.32%)
JavaScript (26.68%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
Download Statistics
Total Downloads
17,211
Last Day
1
Last Week
7
Last Month
45
Last Year
1,650
GitHub Statistics
MIT License
9 Stars
535 Commits
4 Watchers
2 Branches
5 Contributors
Updated on Feb 24, 2025
Bundle Size
12.66 kB
Minified
5.60 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.15.0
Package Id
@sandstreamdev/std@0.15.0
Unpacked Size
311.00 kB
Size
77.44 kB
File Count
649
NPM Version
10.7.0
Node Version
20.15.0
Published on
Jun 24, 2024
Total Downloads
Cumulative downloads
Total Downloads
17,211
Last Day
0%
1
Compared to previous day
Last Week
-72%
7
Compared to previous week
Last Month
-58.3%
45
Compared to previous month
Last Year
-21.4%
1,650
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
std
Installation
1npm install @sandstreamdev/std
Documentation
array
any
Checks if the given array is present and it is not empty (contains at least one element).
Type signature
1<T>(xs?: T[]) => boolean
Examples
1any([]); 2// ⇒ false
1any([1, 2, 3]); 2// ⇒ true
Questions
- How to check if an array is empty?
- How to check if an array is empty or null or undefined?
- How to check if an array is empty or not?
- How to check if an array is empty or doesn't exist?
are
Checks if the given arguments are all Arrays
.
Type signature
1<T>(...xs: T[]) => boolean
Examples
1are([2, 3]); 2// ⇒ true
1are([1, 2, 3], []); 2// ⇒ true
1are([1, 2, 3], 8, [1, 3], "test"); 2// ⇒ false
Questions
- How to check if all the given values are arrays?
chunk
Splits the given array into an array of chunks of up to the given length.
Type signature
1(count: number) => <T>(xs: T[]) => T[] | T[][]
Examples
1chunk(2)(['a', 'b', 'c', 'd']); 2// ⇒ [['a', 'b'], ['c', 'd']]
1chunk(3)(['a', 'b', 'c', 'd']); 2// ⇒ [['a', 'b', 'c'], ['d']]
Questions
- How to split an array into chunks?
- How to split an array into chunks of the same size?
difference
Computes a set difference between the two given arrays.
Type signature
1<T>(xs: T[], ys: T[]) => T[]
Examples
1difference([1, 2, 3, 4, 5, 6], [2, 4]); 2// ⇒ [1, 3, 5, 6]
Questions
- How to find elements which are present in the first array and not in the second?
differs
Checks if two arrays are not equal.
Type signature
1<T>(xs?: T[], ys?: T[]) => boolean
Examples
1differs([1, 2, 3], [1, 2]); 2// ⇒ true
1differs([1, 2, 3], [1, 2, 3]); 2// ⇒ false
Questions
- How to check if two arrays differ?
- How to check if two arrays are not equal?
- How to check if two arrays are equal or not?
duplicates
Lists all the duplicated values in the given array.
Type signature
1<T>(xs: T[]) => T[]
Examples
1duplicates([1, 2, 3, 4, 3, 4, 3, 6]); 2// ⇒ [3, 4, 3]
Questions
- How to find duplicates in an array?
empty
Empty array.
Type signature
1unknown[]
Examples
1empty; 2// ⇒ []
Questions
- How to get an empty array?
exact
Takes exactly the given count of elements.
Type signature
1(count: number) => <T>(xs: T[]) => T[]
Examples
1exact(5)([1, 2, 3]); 2// ⇒ [1, 2, 3, undefined, undefined]
1exact(2)([1, 2, 3]); 2// ⇒ [1, 2]
Questions
- How to get exactly N elements out of an array?
except
Filters out the given value.
Type signature
1<T>(y: T) => (xs: T[]) => T[]
Examples
1except(2)([1, 2, 3, 4, 5]); 2// ⇒ [1, 3, 4, 5]
1except(2)([1, 2, 2, 4, 2]); 2// ⇒ [1, 4]
Questions
- How to get all the values of an array except the given one?
filterInPlace
Filters the given array with the given predicate just like Array.filter but does it in-place thus mutates the original array.
Type signature
1<T>(f: (value: T, index: number, context: T[]) => boolean) => (xs: T[]) => T[]
Examples
1const xs = [1, 2, 3, 4, 5, 6, 7]; 2const odd = x => x % 2 === 1; 3 4const ys = filterInPlace(odd)(xs); 5 6ys === xs; 7// ⇒ true 8ys; 9// ⇒ [1, 3, 5, 7]
Questions
- How to filter an array in place?
find
Finds an element by a predicate function within the given array, otherwise, it returns the given fallback value or undefined when fallback is not present.
Type signature
1<T>(predicate: (value: T, index: number, context: T[]) => boolean, fallback?: T) => (xs: T[]) => T
Examples
1find(x => x > 2)([1, 2, 3, 5, 7]); 2// ⇒ 3
1find(x => x > 2)([1, 2, -3, -5, -7]); 2// ⇒ undefined
Questions
- How to find an element of an array by a given predicate?
first
Returns the first element or undefined when there are no elements in the given array.
Type signature
1<T>([x]: T[]) => T | undefined
Examples
1first([1, 2, 3]); 2// ⇒ 1
1first([]); 2// ⇒ undefined
Questions
- How to get the first element of an array?
flatMap
Maps and flattens the result.
Type signature
1<T, TResult>(f: (value: T, index: number, context: T[]) => TResult[]) => (xs: T[]) => TResult[]
Examples
1flatMap(text => [...text])(["test", "123"]); 2// ⇒ ["t", "e", "s", "t", "1", "2", "3"]
Questions
- How to flat map an array?
- How to map and then flatten an array?
flatten
Flattens the nested arrays by a single level.
Type signature
1<T>(xs: T[]) => T[]
Examples
1flatten([1, [2, 3], 4, [5, 6]]); 2// ⇒ [1, 2, 3, 4, 5, 6]
1flatten([1, [2, [3, 6]], 4, [5, 6]]); 2// ⇒ [1, 2, [3, 6], 4, 5, 6]
Questions
- How to flatten an array?
insert
Inserts the given item to the array at a specific index.
Type signature
1(index: number) => <T>(item: T) => ([...xs]: T[]) => T[]
Examples
1insert(0)('d')(['a', 'b', 'c']); 2// ⇒ ['d', 'a', 'b', 'c']
1insert(1)('d')(['a', 'b', 'c']); 2// ⇒ ['a', 'd', 'b', 'c']
Questions
- How to insert an element to an array at a given position?
intersection
Finds common elements between both arrays.
Type signature
1<T>(xs: T[], ys: T[]) => T[]
Examples
1intersection([1, 2, 3, 4, 5], [5, 5, 3, 2]); 2// ⇒ [2, 3, 5]
Questions
- How to find common elements present in both arrays?
is
Checks if the given argument is an array.
Type signature
1(value?: unknown) => boolean
Examples
1is([1, 2, 3]); 2// ⇒ true
1is({ a: 5 }); 2// ⇒ false
Questions
- How to check if a value is an array?
last
Returns the last element or undefined when there are no elements in the given array.
Type signature
1<T>(xs: T[]) => T | undefined
Examples
1last([1, 2, 3]); 2// ⇒ 3
1last([]); 2// ⇒ undefined
Questions
- How to get the last element of an array?
length
Returns the number of elements in the given array.
Type signature
1<T>(xs: T[]) => number
Examples
1length([true, 1]); 2// ⇒ 2
1length([1, 2, 3]); 2// ⇒ 3
1length([]); 2// ⇒ 0
Questions
- How to check an array's length?
- How to compute an array's length?
- How to check the size of an array?
- How to check the number of elements in an array?
lengthDiffers
Checks if lengths of the given arrays differ.
Type signature
1<T1, T2>(a: T1[], b: T2[]) => boolean
Examples
1lengthDiffers([1, 2, 3], [1, 2]); 2// ⇒ true
1lengthDiffers([6, 7], [1, 2]); 2// ⇒ false
Questions
- How to check if array lengths differ?
- How to check if the given arrays have different lengths?
map
Maps the given array with the given functions.
Type signature
1<T>(...fs: ((x: T) => T)[]) => (xs: T[]) => T[]
Examples
1map(x => x * x)([1, 2, 3]); 2// ⇒ [1, 4, 9]
1map(x => x * x, x => x + 1)([1, 2, 3]); 2// ⇒ [2, 5, 10]
Questions
- How to map an array?
midpoint
Returns the middle element or the right one when the number of elements is even.
Type signature
1<T>(xs: T[]) => T | undefined
Examples
1midpoint([1, 2, 3, 4, 5]); 2// ⇒ 3
1midpoint([1, 2, 3, 4]); 2// ⇒ 3
Questions
- How to get the element in the middle of an array?
- How to get the middle element of an array?
minMax
Computes minimum and maximum values of the given array in a single run.
Type signature
1(xs: number[]) => number[]
Examples
1minMax([10, 5, 3, -5, -4, 23, 32, 8, 1, 0]); 2// ⇒ [-5, 32]
1minMax([1]); 2// ⇒ [1, 1]
1minMax([]); 2// ⇒ [undefined, undefined]
Questions
- How to find the minimum and maximum values of an array?
- How to get the min/max element of an array?
multiple
Checks if the given array contains more than one element.
Type signature
1<T>(xs: T[]) => boolean
Examples
1multiple([1, 2, 3]); 2// ⇒ true
1multiple([1, 2]); 2// ⇒ true
1multiple([1]); 2// ⇒ false
1multiple([]); 2// ⇒ false
Questions
- How to check if an array contains multiple elements?
- How to check whether multiple values exist within an array?
none
Checks if the given array is empty.
Type signature
1<T>(xs?: T[]) => boolean
Examples
1none([]); 2// ⇒ true
1none([1, 2, 3]); 2// ⇒ false
Questions
- How to check if an array is empty?
partition
Partitions the given array to the ones that pass the given predicate function and the ones that do not. By convention of the Haskell's Data.Either, values that pass the predicate are placed at the right.
Type signature
1<T>(predicate: (x: T) => boolean) => (xs: T[]) => readonly [T[], T[]]
Examples
1partition(x => x % 2 === 1)([1, 2, 3, 4, 5]); 2// ⇒ [[2, 4], [1, 3, 5]])
Questions
- How to partition an array based on a condition?
- How to divide an array by a filter function?
pop
Returns the given array without the last element.
Type signature
1<T>(xs: T[]) => T[]
Examples
1pop([1, 2, 3, 4]); // ⇒ [1, 2, 3]
1pop([]); // ⇒ []
Questions
- How to get an array without the last element?
- How to remove the last element from an array?
range
Generates an array of numbers from 0 to n - 1.
Type signature
1(n: number) => number[]
Examples
1range(3); 2// ⇒ [0, 1, 2]
Questions
- How to create an array of all integers from 0 to N exclusive?
removeAt
Removes an element at the given index from the given array.
Type signature
1(index: number) => <T>(xs: T[]) => T[]
Examples
1removeAt(3)([1, 2, 3, 4, 5, 6]) 2// ⇒ [1, 2, 3, 5, 6]
Questions
- How to remove an item from an array at a particular index?
repeat
Repeats the given element by the given count of times.
Type signature
1(count: number) => <T>(value: T) => T[]
Examples
1repeat(3)("test"); 2// ⇒ ["test", "test", "test"]
Questions
- How to repeat a value N times?
reverse
Reverses the given array without mutating it (in contrast to Array.reverse).
Type signature
1<T>(xs: T[]) => T[]
Examples
1reverse([1, 2, 3, 4, 5]); 2// ⇒ [5, 4, 3, 2, 1]
Questions
- How to reverse an array without mutating it?
reverseIf
Reverses the given array when enabled.
Type signature
1(enabled: boolean) => <T>(xs: T[]) => T[]
Examples
1reverseIf(true)([1, 2, 3, 4, 5]); 2// ⇒ [5, 4, 3, 2, 1]
1reverseIf(false)([1, 2, 3, 4, 5]); 2// ⇒ [1, 2, 3, 4, 5]
Questions
- How to reverse an array without mutating it only when a condition is satisfied?
second
Returns the second element or undefined when there are less than two elements in the given array.
Type signature
1<T>(xs: T[]) => T | undefined
Examples
1second([1, 2, 3, 4, 5]); 2// ⇒ 2
1second([1]); 2// ⇒ undefined
1second([]); 2// ⇒ undefined
Questions
- How to get the second element of an array?
secondToLast
Returns the second to last element or undefined when there are less than two elements in the given array.
Type signature
1<T>(xs: T[]) => T | undefined
Examples
1secondToLast([1, 2, 3, 4, 5]); 2// ⇒ 4
1secondToLast([1]); 2// ⇒ undefined
1secondToLast([]); 2// ⇒ undefined
Questions
- How to get the second to last element of an array?
shift
Shifts the given array to the left and circulates the elements back by modulo of the array's length.
Type signature
1(count: number) => <T>(xs: T[]) => T[]
Examples
1shift(1)([1, 2, 3, 4, 5]); 2// ⇒ [2, 3, 4, 5, 1]
1shift(2)([1, 2, 3, 4, 5]); 2// ⇒ [3, 4, 5, 1, 2]
1shift(3)([1, 2, 3, 4, 5]); 2// ⇒ [4, 5, 1, 2, 3]
Questions
- How to shift an array?
shuffle
Shuffles the given array in random order with Math.random as the default.
Type signature
1<T>(xs: T[], random?: () => number) => T[]
Examples
1let i = 0; 2 3const random = () => 4 [ 5 0.013606630487694282, 6 0.21052486239086554, 7 0.28299838254636556, 8 0.696161009199874, 9 0.32165320593537117 10 ][i++]; 11 12shuffle([1, 2, 3, 4, 5], random); // => [3, 5, 4, 2, 1]
Questions
- How to shuffle an array?
shuffleInPlace
Shuffles the given array in-place in random order with Math.random as the default.
Type signature
1<T>(xs: T[], random?: () => number) => T[]
Examples
1let i = 0; 2 3const random = () => 4 [ 5 0.013606630487694282, 6 0.21052486239086554, 7 0.28299838254636556, 8 0.696161009199874, 9 0.32165320593537117 10 ][i++]; 11 12shuffleInPlace([1, 2, 3, 4, 5], random); // => [3, 5, 4, 2, 1]
Questions
- How to shuffle an array in place?
single
Checks if the given array contains exactly one element.
Type signature
1<T>(xs: T[]) => boolean
Examples
1single([1]); 2// ⇒ true
1single([1, 2, 3]); 2// ⇒ false
1single([]); 2// ⇒ false
Questions
- How to check if an array contains only one element?
skip
Skips the given count of elements from the given array.
Type signature
1(count: number) => <T>(xs: T[]) => T[]
Examples
1skip(2)([1, 2, 3, 4, 5]); 2// ⇒ [3, 4, 5]
Questions
- How to skip the first few elements of an array?
slidingWindow
Returns a new array composed of tuples of the given sliding window length of consecutive elements.
Type signature
1(count: number) => <T>(xs: T[]) => T[][]
Examples
1slidingWindow(2)([1, 2, 3, 4]); 2// ⇒ [[1, 2], [2, 3], [3, 4]]
1slidingWindow(3)([1, 2, 3, 4, 5]); 2// ⇒ [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
1slidingWindow(1)([1, 2, 3, 4, 5, 6]); 2// ⇒ [[1], [2], [3], [4], [5], [6]]
Questions
- How to iterate an array pairwise?
sort
Sorts the given array without mutating it.
Type signature
1<T>(f?: (a: T, b: T) => number) => (xs: T[]) => T[]
Examples
1sort((a, b) => a - b)([13, 79, 20, 69, 44, 67, 18, 95, 26, 55]); 2// ⇒ [13, 18, 20, 26, 44, 55, 67, 69, 79, 95]
Questions
- How to sort an array without mutating it?
sum
Sums the given array of numbers.
Type signature
1(xs: number[]) => number
Examples
1sum([1, 2, 3, 4, 5]); 2// ⇒ 15
Questions
- How to sum elements of an array?
take
Takes up to a given count of elements.
Type signature
1(count: number) => <T>(xs: T[]) => T[]
Examples
1take(2)([1, 2, 3, 4, 5]); 2// ⇒ [1, 2]
1take(10)([1, 2, 3, 4, 5]); 2// ⇒ [1, 2, 3, 4, 5]
Questions
- How to get the first N number of elements from an array?
unique
Returns unique elements of the given array.
Type signature
1<T>(xs: T[]) => T[]
Examples
1unique([1, 2, 3, 4, 3, 4, 3, 6]); 2// ⇒ [1, 2, 3, 4, 6]
Questions
- How to find all unique values in an array?
uniqueBy
Filters out duplicated values based on the result of the given key selector.
Type signature
1<T, TResult>(f: (x: T) => TResult) => (xs: T[]) => T[]
Examples
1uniqueBy(({ id }) => id)([ 2 { id: 1, value: 'a' }, 3 { id: 2, value: 'b' }, 4 { id: 1, value: 'c' } 5]) 6// ⇒ [{ id: 1, value: 'c' }, { id: 2, value: 'b' }]
Questions
- How to find all unique values in an array by some predicate?
zip
Zips the given arrays together into pairs.
Type signature
1(xs: unknown[], ys: unknown[]) => [unknown, unknown][]
Examples
1zip([1, 2, 3], [4, 5, 6]); 2// ⇒ [[1, 4],[2, 5],[3, 6]]
Questions
- How to zip two arrays?
zipN
Zips the given arrays together into pairs.
Type signature
1<T>(...xs: T[][]) => T[][]
Examples
1zipN([1, 2, 3], [4, 5, 6]); 2// ⇒ [[1, 4], [2, 5], [3, 6]]
1zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); 2// ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
1zipN([1, 2], [4, 5, 6], [7, 8, 9]); 2// ⇒ [[1, 4, 7],[2, 5, 8]]
Questions
- How to zip multiple arrays?
zipWith
Zips the given arrays together with the given function.
Type signature
1<T1, T2>(f?: (x: T1, y: T2) => [T1, T2]) => (xs: T1[], ys: T2[]) => [T1, T2][]
Examples
1zipWith((x, y) => x * x + y)([1, 2, 3], [4, 5, 6]); 2// ⇒ [5, 9, 15]
Questions
- How to zip two arrays with a given function?
async
debounce
Makes the function run after the given period of not being called. Useful to delay input submission for auto-complete etc.
Type signature
1(f: F, wait: number) => (...args: unknown[]) => void
Examples
1const f = () => console.log("Test"); 2 3const debounced = debounce(f, 2000); 4 5debounced(); 6setTimeout(debounced, 1000); 7setTimeout(debounced, 3000);
Questions
- How to make function fire after some time not being called?
- How to debounce input events?
- How to debounce a function?
delay
When awaited, delays the execution by the given number of milliseconds.
Type signature
1(duration: number) => Promise<unknown>
Examples
1delay(2000)(() => console.log("Test"));
Questions
- How to delay a function?
- What is the JavaScript version of sleep()?
sequence
Runs the given tasks in a sequence.
Type signature
1<T>(tasks: Task<T>[]) => Promise<Awaited<T>[]>
Examples
1const f = () => new Promise(resolve => setTimeout(resolve, 1000)); 2const g = () => new Promise(resolve => setTimeout(resolve, 2000)); 3 4sequence([f, g]).then(() => console.log("Done"));
Questions
- How to run async tasks sequentially?
date
clamp
Clamps the given date to the given date range.
Type signature
1(min: Date, max: Date) => (date: Date) => Date
Examples
1const date = new Date("2019-06-15T13:54:33.232Z"); 2const min = new Date("2019-02-23T13:54:33.232Z"); 3const max = new Date("2019-03-13T13:54:33.232Z"); 4 5clamp(min, max)(date); 6// => new Date("2019-03-13T13:54:33.232Z")
Questions
- How to clamp a date to the desired date range?
- How to enforce a date to be in a given date range?
clone
Clones the given Date object.
Type signature
1(date: Date) => Date
Examples
1const date = new new Date("2019-04-24T13:54:33.232Z"); 2const cloned = clone(date); 3 4cloned !== date && cloned.valueOf() === date.valueOf(); 5// ⇒ true
Questions
- How to clone a Date object?
dateDiff
Computes a signed difference between two Date objects as milliseconds.
Type signature
1(a: Date, b: Date) => number
Examples
1dateDiff(new Date("2017-01-01T13:00:00.000Z"), new Date("2017-01-01T12:00:00.000Z")); 2// ⇒ 3600000
Questions
- How to compute Date difference?
dateInRange
Checks if the given date is between the given date range (inclusive).
Type signature
1(from: Date, to: Date) => (date: Date) => boolean
Examples
1dateInRange(new Date("2018-06-10T12:00:00.000Z"), new Date("2018-06-20T12:00:00.000Z"))(new Date("2018-06-15T12:00:00.000Z")); 2// ⇒ true
Questions
- How to check if a date is within a given date range?
dayRange
Returns a local day range at a particular Date.
Type signature
1(date: Date) => Date[]
Examples
1const date = new Date("2018-12-31T13:54:33.232Z"); 2 3dayRange(date); 4// ⇒ [startOfDay(date), endOfDay(date)]
Questions
- How to find a date range of a given day?
daysInMonths
Returns an array of days in a particular months. Number of days in February varies if it is a leap year or not.
Type signature
1(leapYear: boolean) => [number, number, number, number, number, number, number, number, number, number, number, number]
Examples
1daysInMonths(false); 2// ⇒ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
1daysInMonths(true); 2// ⇒ [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Questions
- How to find out how many days are in a particular month?
- How to find out how many days there are in a leap year?
daysInYear
Calculates the number of days in a particular year. Varies by the leap year.
Type signature
1(year: number) => 366 | 365
Examples
1daysInYear(2019); 2// ⇒ 365
1daysInYear(2020); 2// ⇒ 366
Questions
- How many days are in a particular year?
- How many days are in a leap year?
- How many days are in a common year?
displayTime
Displays padded time string.
Type signature
1(source: [number, number, number], showSeconds: boolean) => string
Examples
1displayTime([5, 12, 16], false); 2// ⇒ 05:12
1displayTime([5, 12, 16], true); 2// ⇒ 05:12:16
Questions
- How to display padded time?
endOfDay
Returns a local Date of an end of the day at a particular Date.
Type signature
1(date: Date) => Date
Examples
1endOfDay(new Date("2018-12-31T13:54:33.232Z")); 2// ⇒ new Date(new Date("2019-01-01T00:00:00.000Z").valueOf() + new Date("2018-12-31T13:54:33.232Z").getTimezoneOffset() * 60 * 1000)
Questions
- How to find a date of an end of a given day?
formatDate
Formats a given date as a simple YYYY-MM-DD string.
Type signature
1(date: Date) => string
Examples
1formatDate(new Date("2019-02-24T01:12:34")); 2// ⇒ "2019-02-24"
Questions
- How to render a date in a YYYY-MM-DD format?
formatDateTime
Formats a given date as a simple YYYY-MM-DD HH:MM(:SS) string.
Type signature
1(sourceDate: Date, showSeconds?: boolean) => string
Examples
1formatDateTime(new Date("2019-02-24T01:12:34")); 2// ⇒ "2019-02-24 01:12"
1formatDateTime(new Date("2019-02-24T01:12:34"), true); 2// ⇒ "2019-02-24 01:12:34"
Questions
- How to render a date in a YYYY-MM-DD HH:MM format?
- How to render a date in a YYYY-MM-DD HH:MM:SS format?
formatDuration
Formats a duration in milliseconds to a padded time string.
Type signature
1(duration: number, showSeconds?: boolean) => string
Examples
1formatDuration(26100000); 2// ⇒ 07:15
1formatDuration(26136000, true); 2// ⇒ 07:15:36
Questions
- How to render a formatted duration?
formatTime
Formats a given date as a simple HH:MM(:SS) string.
Type signature
1(date: Date, showSeconds?: boolean) => string
Examples
1formatTime(new Date("2019-02-24T01:12:34")); 2// ⇒ "01:12"
1formatTime(new Date("2019-02-24T01:12:34"), true); 2// ⇒ "01:12:34"
Questions
- How to render a date in a HH:MM format?
- How to render a date in a HH:MM:SS format?
fromDays
Converts the given day count to milliseconds.
Type signature
1(days: number) => number
Examples
1fromDays(1); 2// ⇒ 86400000
Questions
- How to find how many milliseconds are in a given number of days?
fromHours
Converts the given hour count to milliseconds.
Type signature
1(hours: number) => number
Examples
1fromHours(1); 2// ⇒ 3600000
Questions
- How to find how many milliseconds are in a given number of hours?
fromMinutes
Converts the given minute count to milliseconds.
Type signature
1(minutes: number) => number
Examples
1fromMinutes(1); 2// ⇒ 60000
Questions
- How to find how many milliseconds are in a given number of minutes?
fromSeconds
Converts the given second count to milliseconds.
Type signature
1(seconds: number) => number
Examples
1fromSeconds(1); 2// ⇒ 1000
Questions
- How to find how many milliseconds are in a given number of seconds?
joinDateTime
Joins a date-time pair into a date-time string.
Type signature
1(date: string, time: string) => string
Examples
1joinDateTime("2019-01-15", "13:54:33.232Z"); 2// ⇒ "2019-01-15T13:54:33.232Z"
Questions
- How to join date and time to get ISO-compliant date-time string?
leapYear
Detects if a given year is a leap year.
Type signature
1(year: number) => boolean
Examples
1leapYear(2020); 2// ⇒ true
1leapYear(2019); 2// ⇒ false
Questions
- How to find if the given year is a leap year?
parseHourMinutePair
Parses HH:MM string into hours and minutes.
Type signature
1(text?: string) => [number, number]
Examples
1parseHourMinutePair("12:34"); 2// ⇒ [12, 34]
Questions
- How to parse time string into hours and minutes?
splitDateTime
Splits a date-time string into a date-time pair.
Type signature
1(dateTimeString: string) => [string, string]
Examples
1splitDateTime("2019-01-15T13:54:33.232Z"); 2// ⇒ ["2019-01-15", "13:54:33.232Z"]
Questions
- How to split ISO-compliant date-time string into a date and time pair?
startOfDay
Returns a local Date of a start of the day at a particular Date.
Type signature
1(date: Date) => Date
Examples
1endOfDay(new Date("2019-01-01T13:54:33.232Z")); 2// ⇒ new Date(new Date("2019-01-01T00:00:00.000Z").valueOf() + new Date("2019-01-01T13:54:33.232Z").getTimezoneOffset() * 60 * 1000)
Questions
- How to find a date of the start of a given day?
subtractDays
Subtracts the given number of days from the given Date object.
Type signature
1(sourceDate: Date, numberOfDays: number) => Date
Examples
1subtractDays(new Date("2019-01-15T13:54:33.232Z"), 1); 2// ⇒ new Date("2019-01-14T13:54:33.232Z")
Questions
- How to subtract days from a given date?
toDate
Extracts padded YYYY-MM-DD date string out of the given date object.
Type signature
1(date: Date) => string
Examples
1toDate(new Date("2019-01-15T12:00:00.000Z")); 2// ⇒ "2019-01-15"
Questions
- How to get only the date from a Date object?
toDates
Converts the given array of values into Dates using the Date constructor.
Type signature
1(xs: (string | number | Date)[]) => Date[]
Examples
1toDates(["2019-01-15T13:54:33.232Z", new Date("2019-01-15T13:54:33.232Z").valueOf(), new Date("2019-01-15T13:54:33.232Z")]); 2// ⇒ [new Date("2019-01-15T13:54:33.232Z"), new Date("2019-01-15T13:54:33.232Z"), new Date("2019-01-15T13:54:33.232Z")]
Questions
- How to convert an array of string and timestamps into an array of Date objects?
toDays
Converts milliseconds into days.
Type signature
1(milliseconds: number) => number
Examples
1toDays(86400000); 2// ⇒ 1
Questions
- How to convert milliseconds into days?
toHours
Converts milliseconds into hours.
Type signature
1(milliseconds: number) => number
Examples
1toHours(3600000); 2// ⇒ 1
Questions
- How to convert milliseconds into hours?
toISO
Returns an ISO-compliant date-time string.
Type signature
1(x: Date) => string
Examples
1toISO(new Date("2019-04-24T13:54:33.232Z")); 2// ⇒ "2019-04-24T13:54:33.232Z"
Questions
- How to convert Date object to ISO-compliant date string?
toMinutes
Converts milliseconds into minutes.
Type signature
1(milliseconds: number) => number
Examples
1toMinutes(60000); 2// ⇒ 1
Questions
- How to convert milliseconds into minutes?
toSeconds
Converts milliseconds into seconds.
Type signature
1(milliseconds: number) => number
Examples
1toSeconds(1000); 2// ⇒ 1
Questions
- How to convert milliseconds into seconds?
valid
Checks if the given date is present and it is valid.
Type signature
1(date?: unknown) => boolean
Examples
1valid(new Date("2020-01-31T09:52:31.618Z")); 2// ⇒ true
1valid(new Date("2020-01-42:52:31.618Z")); 2// ⇒ false
1valid(new Date("test")); 2// ⇒ false
1valid(undefined); 2// ⇒ false
Questions
- How to check if a Date is valid or not?
debug
assert
Asserts given conditions.
Type signature
1(condition: boolean, callbackOrMessage?: (() => void) | string) => void; 2export declare const throws: (f: () => void) => unknown | undefined; 3export declare const assertNumber: (x?: unknown) => void; 4export declare const assertInteger: (x?: unknown) => void; 5export declare const assertByte: (x?: unknown) => void; 6export declare const assertNormal: (x?: unknown) => void; 7export declare const assertString: (x?: unknown, message?: string) => void; 8export declare const assertIsDefined: (x?: unknown, message?: string) => void; 9export default assert
Examples
1assert(true === false); 2// ⇒ TypeError("Assertion failed!")
Questions
- How to assert a condition?
- How to throw when a condition is not satisfied?
diff
Computes a deep difference between the two values (primitives, objects, arrays, etc.).
Type signature
1(obj1?: unknown, obj2?: unknown) => DiffResult; 2export default diff
Examples
1diff({ a: 1 }, { a: 2 }); 2// ⇒ { a: { data: [1, 2], type: '~' }}
Questions
- How to compute a diff?
- How to compute a deep diff?
- How to compute a diff between two objects?
- How to compute a diff between two arrays?
encoding
base64url
decode
Decodes the given Base64URL back into a string.
Type signature
1(text: string, context?: DecodeContext) => string
Examples
1decode("PDw_Pz8-Pg"); 2// ⇒ "<<???>>"
Questions
- How to decode Base64URL?
decodeBytes
Decodes the given Base64URL back into a byte array.
Type signature
1(text: string, context?: DecodeContext) => number[]
Examples
1decodeBytes("w4Jnw6vCp20-bBsQfA"); 2// ⇒ [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]
Questions
- How to decode Base64URL into a byte array?
encode
Encodes the given string into Base64URL.
Type signature
1(text: string, context?: EncodeContext) => string
Examples
1encode("<<???>>"); 2// ⇒ "PDw_Pz8-Pg"
Questions
- How to encode a string as Base64URL?
encodeBytes
Encodes the given bytes into Base64URL.
Type signature
1(bytes: number[], context?: EncodeContext) => string
Examples
1encodeBytes([0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]); 2// ⇒ "w4Jnw6vCp20-bBsQfA"
Questions
- How to encode bytes as Base64URL?
fromBase64
Converts Base64 string into Base64URL one.
Type signature
1(base64: string) => string
Examples
1fromBase64("PDw/Pz8+Pg=="); 2// ⇒ "PDw_Pz8-Pg"
Questions
- How to convert Base64 to Base64URL?
toBase64
Converts Base64URL string into Base64 one.
Type signature
1(base64Url: string) => string
Examples
1toBase64("PDw_Pz8-Pg"); 2// ⇒ "PDw/Pz8+Pg=="
Questions
- How to convert Base64URL to Base64?
byteString
from
Converts a string to a byte array.
Type signature
1(byteString: string) => number[]
Examples
1from("PQR"); 2// ⇒ [80, 81, 82]
Questions
- How to convert a string into a byte array?
to
Coverts a byte array into a string.
Type signature
1(bytes: number[]) => string
Examples
1to([0x50, 0x51, 0x52]); 2// ⇒ "PQR"
Questions
- How to convert a byte array to string?
file
validName
Checks if the given string is a valid Windows file name.
Type signature
1(name: string) => boolean
Examples
1validName("my:file.png"); 2// ⇒ false
1validName("file.txt"); 2// ⇒ true
1validName("../file.txt"); 2// ⇒ false
1validName("COM1"); 2// ⇒ false
Questions
- How to find valid Windows file name?
- How to check if a given string is a legal/valid file name under Windows?
function
compose
Composes multiple functions into a higher-order one. Goes right to left.
Type signature
1<T, TResult>(...fs: ((x: T) => T)[]) => (x: T) => T
Examples
1compose(x => x * x, x => x + 1)(3); 2// ⇒ 16
Questions
- How to compose functions?
constant
Returns the given constant no matter the input.
Type signature
1<T>(x: T) => () => T
Examples
1constant(3)("anything"); 2// ⇒ 3
Questions
- How to create a function that always returns the same value despite given arguments?
identity
Always return the given value.
Type signature
1<T>(x: T) => T
Examples
1identity(5); 2// ⇒ 5
1identity("test"); 2// ⇒ "test"
Questions
- How to use the identity function?
- Where and why is identity function useful?
memoize
Memoizes the function result so it is not computed for the same parameters. Uses deep equality.
Type signature
1<TResult>(f: (...xs: unknown[]) => TResult) => (...args: unknown[]) => TResult
Examples
1const f = x => { console.log(x); return x + 1; }; 2 3const memoized = memoize(f); 4 5memoized(5); 6memoized(5); 7memoized(5); 8memoized(3);
Questions
- How to memoize a function?
memoizeShallow
Memoizes the function result so it is not computed for the same parameters. Uses shallow equality.
Type signature
1<TResult>(f: (...xs: unknown[]) => TResult) => (...args: unknown[]) => TResult
Examples
1const f = ({ x }) => { console.log(x); return x + 1; }; 2 3const memoized = memoizeShallow(f); 4 5memoized({ x: 5 }); 6memoized({ x: 5 }); 7memoized({ x: 5 }); 8memoized({ x: 3 });
Questions
- How to memoize a function with shallow equality?
memoizeWith
Memoizes the function result so it is not computed for the same parameters. Uses the given equality function.
Type signature
1<T>(equals: (x: T[], y: T[]) => boolean) => <TResult>(f: (...xs: T[]) => TResult) => (...args: T[]) => TResult
Examples
1const f = ({ x }) => { console.log(x); return x + 1; }; 2 3const memoized = memoizeWith((a, b) => a.x === b.x)(f); 4 5memoized({ x: 5 }); 6memoized({ x: 5 }); 7memoized({ x: 5 }); 8memoized({ x: 3 });
Questions
- How to memoize a function with a custom equality function?
noOp
It does exactly nothing.
Type signature
1() => void
Examples
1noOp("anything"); 2// ⇒ undefined
Questions
- How to create a function that does nothing?
not
Inverts the given function result.
Type signature
1(f: (...xs: unknown[]) => unknown) => (...args: unknown[]) => boolean
Examples
1not(x > 10)(15); 2// ⇒ true
Questions
- How to invert a boolean function?
pipe
Pipes an input through given functions.
Type signature
1<T>(...fs: ((x: T) => T)[]) => (x: T) => T
Examples
1pipe(x => x * x, x => x + 1)(3); 2// ⇒ 10
Questions
- How to pipe an argument through a function?
when
Runs the given function only when the condition is met.
Type signature
1(predicate: (...xs: unknown[]) => boolean) => (action: (...xs: unknown[]) => unknown) => (...args: unknown[]) => unknown
Examples
1when(x => x > 0)(x => console.log(x))(5); 2when(x => x > 0)(x => console.log(x))(-3);
Questions
- How to run a function only when a condition is satisfied?
whenTrue
Runs the given function only when the condition is exactly true.
Type signature
1(action: (...xs: unknown[]) => unknown) => (...args: unknown[]) => unknown
Examples
1whenTrue(x => console.log(x))(false); 2when(x => x > 0)(x => console.log(x))(true);
Questions
- How to run a function only if its argument is true?
- How to execute function only if a variable is true?
is
array
Checks if the given argument is an array.
Type signature
1(x?: unknown) => boolean
Examples
1array([1, 2, 3]); 2// ⇒ true
1array({ a: 1 }); 2// ⇒ false
Questions
- How to check if a given value is an array?
boolean
Checks if the given value is a boolean.
Type signature
1(x?: unknown) => x is boolean
Examples
1boolean(false); // ⇒ true
1boolean(1); // ⇒ false
Questions
- How to check if a given value is a boolean?
byte
Checks if the given value is a byte.
Type signature
1(x?: unknown) => boolean
Examples
1byte(128); 2// ⇒ true
1byte(325); 2// ⇒ false
1byte(65.5); 2// ⇒ false
Questions
- How to check if a given value is a byte?
- How to check if a given number is a byte?
date
Checks if the given value is a Date object.
Type signature
1(x?: unknown) => boolean
Examples
1date(new Date()); 2// ⇒ true
1date(123); 2// ⇒ false
Questions
- How to check if a given value is a Date object?
defined
Checks if the given value is defined.
Type signature
1(x?: unknown) => boolean
Examples
1defined(undefined); 2// ⇒ false
1defined(null); 2// ⇒ true
1defined(0); 2// ⇒ true
1defined({ a: 1 }); 2// ⇒ true
Questions
- How to check if a given value is defined?
- How to check if a given value is not undefined?
function
Checks if the given value is a function.
Type signature
1(x?: unknown) => x is Function
Examples
1_function(x => x + 5); 2// ⇒ true
Questions
- How to check if a given value is a function?
integer
Checks if the given value is an integer.
Type signature
1(x?: unknown) => boolean
Examples
1integer(5); 2// ⇒ true
1integer(32.5); 2// ⇒ false
Questions
- How to check if a given value is an integer?
- How to check if a given number is an integer?
nonNullable
Checks and asserts the given value is not null or undefined.
Type signature
1<T>(val: T) => val is NonNullable<T>
Examples
1nonNullable(null); 2// ⇒ false
1nonNullable(undefined); 2// ⇒ false
1nonNullable(false); 2// ⇒ true
1nonNullable({ a: 1 }); 2// ⇒ true
Questions
- How to check if a given value is non-nullable?
- How to check if a given value is not null?
- How to check if a given value is not undefined?
normal
Checks if the given value is a number in a normal range [0, 1].
Type signature
1(x?: unknown) => boolean
Examples
1normal(0.75); 2// ⇒ true
1normal(-1); 2// ⇒ false
1normal(2.5); 2// ⇒ false
Questions
- How to check if a given value is in 0 to 1 inclusive range?
number
Checks if the given value is a number.
Type signature
1(x?: unknown) => boolean
Examples
1number(0 / 0); 2// ⇒ false
1number(15.6); 2// ⇒ true
Questions
- How to check if a given value is a valid number?
- How to check if a given value is not NaN?
- How to check if a given value is finite?
object
Checks if the given value is an object.
Type signature
1(x?: unknown) => boolean
Examples
1object({ a: 1, b: 2 }); 2// ⇒ true
1object([1, 2, 3]); 2// ⇒ false
Questions
- How to check if a given value is an object?
string
Checks if the given value is a string.
Type signature
1(x?: unknown) => x is string
Examples
1string("Test"); 2// ⇒ true
1string(['T', 'e', 's', 't']); 2// ⇒ false
Questions
- How to check if a given value is a string?
math
add
Adds two values.
Type signature
1(a: number, b: number) => number
Examples
1add(3, 5); 2// ⇒ 8
Questions
- How to add two values?
average
Calculates the average of the given array of numbers.
Type signature
1(xs?: number[]) => number
Examples
1average([2, 4, 15]); 2// ⇒ 7
Questions
- How to compute the average of an array?
ceilToNearestPowerOfTwo
Finds the nearest power of two greater or equal to the given value.
Type signature
1(x: number) => number
Examples
1ceilToNearestPowerOfTwo(345); 2// ⇒ 512
Questions
- How to get the nearest power of two greater or equal to the given value?
clamp
Clamps the given value to the given range.
Type signature
1(min: number, max: number) => ((x: number) => number)
Examples
1clamp(0, 10)(5); 2// ⇒ 5
1clamp(0, 10)(-5); 2// ⇒ 0
1clamp(0, 10)(15); 2// ⇒ 10
Questions
- How to clamp value to the desired range?
- How to enforce a value to be in a given range?
clampNormal
Clamps the given value to the [0, 1] range.
Type signature
1(x: number) => number
Examples
1clampNormal(0.5); 2// ⇒ 0.5
1clampNormal(-0.5); 2// ⇒ 0
1clampNormal(1.5); 2// ⇒ 1
Questions
- How to clamp value to be in 0 to 1 inclusive range?
- How to clamp value to be in the normal range?
clampPercentage
Clamps the given value to the [0, 100] range.
Type signature
1(x: number) => number
Examples
1clampPercentage(50); 2// ⇒ 50
1clampPercentage(-50); 2// ⇒ 0
1clampPercentage(150); 2// ⇒ 100
Questions
- How to enforce a percentage be between 0% and 100%?
delta
Calculates the absolute distance between given values.
Type signature
1(a: number, b: number) => number
Examples
1delta(-3, 5); 2// ⇒ 8
Questions
- How to calculate an absolute distance between two numbers?
inRectangleRange
Checks if the given value is in the rectangular range of [0, width] and [0, height]
Type signature
1(width: number, height: number) => ((x: number, y: number) => boolean)
Examples
1inRectangleRange(50, 100)(25, 50); 2// ⇒ true
1inRectangleRange(50, 100)(-25, 50); 2// ⇒ false
Questions
- How to check if a point is inside a rectangle defined by width and height?
lerp
Linearly interpolates two given values by the normal value of their distance.
Type signature
1(t: number) => ((a: number, b: number) => number)
Examples
1lerp(0.5)(0, 10); 2// ⇒ 5
1lerp(0)(0, 10); 2// ⇒ 0
1lerp(1)(0, 10); 2// ⇒ 10
Questions
- How to linearly interpolate between two values?
- How to interpolate two numbers?
maximumBy
Calculates the maximum by a given selector.
Type signature
1(f: (x: number) => number) => ((xs: number[]) => number)
Examples
1maximumBy(({ age }) => age)([{ age: 13 }, { age: 20 }, { age: 7 }, { age: 18 }]); 2// ⇒ { age: 20 }
Questions
- How to find a maximum element by a given function?
median
Calculates the median of the values. If there is an even number of items, the average of the middle ones is returned.
Type signature
1(xs?: number[]) => number | undefined
Examples
1median([-5, 3, 2, 29, 43]); 2// ⇒ 3
Questions
- How to compute a median of an array?
minMax
Calculates the minimum and maximum value of the two given values.
Type signature
1([a, b]: [number, number]) => [number, number]
Examples
1minMax([5, 3]); 2// ⇒ [3, 5]
1minMax([3, 5]); 2// ⇒ [3, 5]
Questions
- How to get ordered values where the lower is the first and the higher is the second?
sameSign
Checks if all the given values have the same sign.
Type signature
1(xs: number[]) => boolean
Examples
1sameSign([-1, -2, -3]); 2// ⇒ true
1sameSign([1, 2, -3]); 2// ⇒ false
Questions
- How to check if all values have the same sign?
sign
Calculates the sign of the value and returns -1 for negative values, 1 for positive values and 0 for zeros.
Type signature
1(x: number) => number
Examples
1sign(3); 2// ⇒ 1
1sign(-5); 2// ⇒ 5
1sign(0); 2// ⇒ 0
1sign(-0); 2// ⇒ 0
Questions
- How to get a sign of a number?
standardDeviation
Calculates the standard deviation of the given array of numbers.
Type signature
1(xs: number[], origin?: number) => number
Examples
1standardDeviation([96, 81, 68, 79, 23, 13, 13, 59, 44, 86]); 2// ⇒ (2 * Math.sqrt(10922 / 5)) / 3
Questions
- How to compute a standard deviation of an array?
subtract
Subtracts two values.
Type signature
1(a: number, b: number) => number
Examples
1subtract(3, 5); 2// ⇒ -2
Questions
- How to subtract two numbers?
object
any
Checks if the given object is present and it is not empty (contains at least one entry).
Type signature
1<T>(xs?: GenericObject<T>) => boolean
Examples
1any({ a: 1, b: 2, c: 3 }); 2// ⇒ true
1any({ }); 2// ⇒ false
1any(null); 2// ⇒ false
1any(undefined); 2// ⇒ false
Questions
- How to check if an object is not empty?
- How to check if an object contains some values?
- How to check if an object is not null or undefined?

No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
Found 2/7 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deploy.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/sandstreamdev/std/deploy.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deploy.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/sandstreamdev/std/deploy.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deploy.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/sandstreamdev/std/deploy.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/sandstreamdev/std/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/sandstreamdev/std/nodejs.yml/master?enable=pin
- Info: 0 out of 5 GitHub-owned GitHubAction dependencies pinned
- Info: 2 out of 2 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/deploy.yml:1
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 28 are checked with a SAST tool
Score
3.9
/10
Last Scanned on 2025-03-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More