'one' is a JavaScript library purposed for serving as an alias space for tiny and frequently recurring tasks.
npm install @hgargg-0710/one
The library provides organizes its exports in separate modules.
They are:
array
string
function
tree
object
set
map
number
function lastOut(x: any[]): any[]
A function that returns a copy of the given array without its last element.
function last(x: any[]): any
A function that returns an array's last element.
function clear(x: any[]): 0
Sets the array's length to 0
, returning it.
function insert(x: any[], index: number, ...values: any[]): any[]
Inserts the values
into x
at index index
and returns the result (new array is created).
function replace(arr: any[], index: number, ...values: any[]): any[]
Replaces the element of arr
at index
with values
(creates a new array).
function out(arr: any[], index: number): any[]
Returns a copy of arr
, in which the element at position index
is absent.
function swapped(x: any[], i: number, j: number): any[]
Returns a new array in which x
values at indexes i
and j
are swapped places.
function firstOut(x: any[]): any[]
Returns the copy of the given array without the first element (x[0]
).
function first(x: any[]): any
Returns the first element of the given array.
function propPreserve(f: (x: object): any[]): (x: object): any[]
Creates and returns a new function based off the givn one that preserves the non-numeric properties
of the argument x
upon the result of f(x)
.
Intended for convertion of functions in terms of array to be able to preserve the custom properties (and thus, be able to use arrays the same way as one would normally do with plain objects).
function iterator(x: any[]): GeneratorFunction
Creates and returns a new iterator function defined in terms of the given array.
function middleOutP(x: any[]): any[]
Discards the middle element from the copy of original array
(with preference to the latter half in even-.length
ed ones),
and returns the copy.
function middleOutN(x: any[]): any[]
Same as middleOutP
, but with preference to the former part in
even-.length
ed arrays.
function capitalize(x: string): string
Returns capitalized version of the string - first character is brought to its upper case, while the rest are brought to their lower case.
function extract(x: string, toExtract: string | RegExp): string
Returns the new string derived from x
without the appearences of toExtract
.
function count(x: string, sub: string | RegExp): number
Counts the number of appearences of substring sub
inside of x
.
function limit(maxsize: number, limitor?: string): (x: string): string
A function returning a function that replaces the remainder of x
after the maximum allowed length of maxsize
with string limitor
.
function ndepth(f: Function): (n: number, ...argsArr: any[]): any
Returns a sequence of nested function with call depth of n
that reduces to f(x1, x2, ..., xn, ...argsArr)
.
NOTE: same as doing manually:
// * was
const a = (x, y) => x + y
const b = (x) => (y) => x + y // b == ndepth(a)(2)
Useful for treating multivariable functions as a call-sequence of single-variable ones.
function or(...fs: Function[]): (...x: any[]): any
Iterates over the list of functions fs
, returning the most truthy value of the returned (otherwise, the first function's value is returned).
function and(...fs: Function[]): (...x: any[]): any
The 'and'-counterpart of the or
function.
function trivialCompose(...fs: Function[]): (...x: any[]): any
Returns a result of composition of single-variable functions with a (possibly) multi-variable one (last function).
The calling of the result is the same as f1(f2(...(fn(...x))))
.
function iterations(f: Function, n: number, j?: number): any[]
Returns the result of iterations of function f
from 0
to n - 1
with a jump j
(1 by default).va
function sequence(f: Function, n: number): (...args: any[]): any[]
Returns the list of results of 1-variable iteration of f
upon itself from 1
to n
times.
function repeat(f: Function, n: number): void
Calls the function f
for values from 0
to n-1
.
function arrayCompose(...fs: ((...x: any[]): any[])[]): (...x): any
A function that accepts an array of functions, each of which returns an array. The functions are then composed in a fashion that uses the output of each (an array) as signature for the next function (that being, the result of the next function are expanded).
function cache(f: (x: any): any, keys: any[]): Map
Creates a new Map
, with keys of keys
and values of f(key)
for each key
in keys
.
This, effectively, caches a portion of a function so that its cached values can be retrieved like:
const f = ...
const cached = cache(f, ...)
const value = c.get(...)
function tuplePick(...inds: ((x: any, i: number, arr: any[]): boolean)[]): (...fs: (...x: any[]): any): (...x: any[]): any
A function that returns a sequence of functions that result in a tuple consisting of
values returned from each of functions from fs
function array, with values
being from x
array, and i
th function only using the values x.filter((inds[i] || (() => true))
.
function tupleSlice(...inds: [number?, number?][]): (...fs: (...x: any[]): any): (...x: any[]): any
Similar to tuplePick
, but operates on .slice
-ranges from inds
instead
(the array of indexes for i
th function is sliced using inds[i]
).
NOTE: here 'trees' are nested arrays.
function depth(tree: any[]): number
Returns the maximum depth of a tree.
function treeFlatten(tree: any[]): any[]
Returns the completely flattened tree.
function recursiveIndexation(tree: any[], multindex: number[]): any
Returns the value obtained after recursively indexing the tree using the values from multindex
number array.
function recursiveSetting(tree: any[], multindex: number[], value: any): any
Sets the value of tree
at multi-index multindex
to value
.
function treeCount(
tree: any[],
prop: (x: any): boolean | number | string,
start?: any
): boolean | number | string
Returns the 'recursive sum' of all the elements inside the tree (can be a kind of count or a string concatenation).
The default return value, start
(by default, 0
), is appended every time a level is passed.
function levelCount(tree: any[]): number
Counts the number of levels inside the tree (including the initial one).
function deepSearch(tree: any[], prop: (x: any): boolean): number[]
Finds the first value x
inside the tree
such that !!prop(x) == true
and returns its multi-index.
function treeReverseLR(tree: any[]): any[]
Reverses the tree from "left" to "right" - recursively applies x.reverse()
upon each of its elements, returns the tree's copy.
function kv(x: object): [string[], any[]]
Returns the pair of key-values of the given object.
function dekv(x: [string[], any[]]): object
Reverses the kv
.
function structCheck(...props: (string | string[])[]): (x: any): boolean
Creates and returns a new function that checks whether:
typeof x === 'object'
!!x
props.every((p) => p in x)
Allows to create handy predicates for checking structural adherence of x
to
a certain necessary objects' "class" without needing to use more complex constructions
like prototype chains.
function toMap(x: object): Map
Converts the given object to a Map
.
function same(a: Set, b: Set): boolean
Checks if sets are the same.
function norepetitions(x: any[]): any[]
Returns a new array with the elements of x
, but without repetitions.
function kv(map: Map): [any[], any[]]
A Map
version of object.kv
.
function dekv(x: [any[], any[]]): Map
A Map
version of object.dekv
.
function toObject(map: Map): object
Converts the given Map
to an object
.
function sum(...x: string[] | number[]): number | string
Returns the sum of the given items x
.
function product(...x: number[]): number
Returns the product of the given items x
.