0.0.74 • Published 3 months ago

@bruyland/utilities v0.0.74

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Medisch Labo Bruyland Utility library

Origin

Some parts of this library have been copied from the - no longer maintained - radash library, the typing resolution of the radash functions has been greatly improved while the functionality has mostly remained the same.

Documentation

Use the radash documentation for these functions

FunctionGroupDescription
tryitasynctry an async function without forking the control flow
uniquearrayGiven a list of items returns a new list with only unique items
boilarrayGo through a list of items, starting with the first item, and comparing with the second. Keep the one you want then compare that to the next item in the list with the same.
selectarrayperforms a filter and a mapper inside of a reduce
maxarraygets the greatest value from a list
minarraygets the smallest value from a list
rangearraycreates a generator that will produce an iteration through a range of numbers
listarraycreates a list of given start, end, value, and step parameters
mapKeysobjectmap over all the keys of an object to return a new object with altered property names
mapValuesobjectreturns a new object with all the values mapped through the toValue function
mapEntriesobjectcombines mapKeys and mapValues in one pass
cloneobjectcreates a shallow copy
getobjectget a value from a nested object using the JSON path
setobjectsets a value on an object using a JSON path
crushobjectflattens a deep object to a single demension, converting the keys to JSON path notation
constructobjectthe opposite of crush
listifyobjectreturns an array with an item for each entry in the object
pickobjectgiven an object and a list of keys in the object, returns a new object with only the given keys
omitobjectomits a list of properties from an object
isSymboltype
isArraytype
isObjecttype
isPrimitivetypeChecks if the given value is primitive
isFunctiontype
isStringtype
isInttype
isFloattype
isNumbertype
isDatetype
isPromisetype
isEmptytype
isEqualtype
toFloatnumber
tointnumber

non-radash functions

try helpers

type Left<E = Error> = [E, undefined]
type Right<T = any> = [undefined, T]
type Either<E = Error, T = any> = Left<E> | Right<T>
type Task<E = Error, T = any> = Promise<[E, undefined] | [undefined, Awaited<T>]>
type TaskOrEither<E = Error, T = any> = T extends Promise<any> ? Task<E, T> : Either<E, T>

const isLeft = <E = Error, T = any>(either: Either<E, T>): boolean =>
  isArray(either) && either[0] !== undefined
const isRight = <E = Error, T = any>(either: Either<E, T>): boolean =>
  isArray(either) && either[0] === undefined
const left = <E = Error>(either: Either<any, E>): E => either[0] as E
const right = <T = any>(either: Either<Error, T>): T => either[1] as T

handle

delegates handling of errors to a handler function

// signature
const result = handle<T,E>(
  fn: (...args: Args) => T,
  handler: (error: E) => void,
)

// use
const errorHandler = (error) => {
  // do whatever stuff you have to do for handling the error
}
handle(errorHandler, () => {
  //... do stuff that may or may not throw
  // where different errors can all be handled 
  // with the same error handler
})(...args)
// or
const result = handle(errorHandler, (...args) => {
  // some code that generates a result
  // the function will return undefined
  // if an error was throw and handled
})

Works both with sync ans async functions

diff

Calculates the differences between two object with equal types.

diff returns an object with a change field and a differences field. change can be add, update, delete or none. The differences field will contain an object detailing the differences between the first and the second parameter.

example

const old = { species: 'cat', name: 'Eline', age: 10, weight: 4.5 }
const _new = { species: 'cat', name: 'Eline', age: 11, weight: 4.2 }

const d1 = diff(old, _new)
/*{
  change: 'update',
  differences: { old: { age: 10 }, new: { age: 11 } }
}*/

const d2 = diff(undefined, _new, { reportOnAdd: ['name', 'age'] })
/*
{ change: 'add', differences: { name: 'Eline', age: 11 } }
*/

const d3 = diff(undefined, _new, { reportOnAdd: ['name', 'species'] })
/* 
{ change: 'add', differences: { name: 'Eline', species: 'cat' } }
*/

The layout of the third parameter (options) is

interface DiffOptions<T> {
  ignoreOnUpdate?: (keyof T)[]
  reportOnAdd?: (keyof T)[]
  reportOnDelete?: (keyof T)[]
  aName?: string
  bName?: string
}
  • ignoreOnUpdate: the difference of the fields with these name will not be included in the differences object
  • ignoreOnUpdate: only these fields will be reported when an add operation was detected. All fields will be reported when ignoreOnUpdate is []
  • reportOnDelete: only these fields will be reported when an delete operation was detected. All fields will be reported when ignoreOnUpdate is []
  • aName and bName: names for the objects in the differences object. By default, a is named 'old' and b is named 'new'

update

Chainable object updater that reports the changes made to the object.

update will return the same differences object like the diff function. The differenc being that update mutates the origional object in place. The same options can be used to control the reporting.

example

const updates = old.update({ age: 9, weight: 4.1 }, { alwaysReport: ['name'], ignoreOnUpdate: ['weight'] })
/*
{ old: { age: 10, name: 'Eline' }, new: { age: 9 } }
*/

The updates object can be passed from one call to another, new content will be added to this object with every call.

Library template

created acording to https://dev.to/0xkoji/create-a-npm-package-template-with-typescript-and-tsup-328n

0.0.73

3 months ago

0.0.74

3 months ago

0.0.72

4 months ago

0.0.71

4 months ago

0.0.70

4 months ago

0.0.68

4 months ago

0.0.69

4 months ago

0.0.62

5 months ago

0.0.63

5 months ago

0.0.64

5 months ago

0.0.65

5 months ago

0.0.66

5 months ago

0.0.67

5 months ago

0.0.60

5 months ago

0.0.61

5 months ago

0.0.59

5 months ago

0.0.55

5 months ago

0.0.56

5 months ago

0.0.57

5 months ago

0.0.58

5 months ago

0.0.53

5 months ago

0.0.54

5 months ago

0.0.41

5 months ago

0.0.42

5 months ago

0.0.43

5 months ago

0.0.44

5 months ago

0.0.45

5 months ago

0.0.46

5 months ago

0.0.47

5 months ago

0.0.51

5 months ago

0.0.52

5 months ago

0.0.50

5 months ago

0.0.48

5 months ago

0.0.49

5 months ago

0.0.40

5 months ago

0.0.38

5 months ago

0.0.39

5 months ago

0.0.37

5 months ago

0.0.35

5 months ago

0.0.36

5 months ago

0.0.30

5 months ago

0.0.31

5 months ago

0.0.32

5 months ago

0.0.33

5 months ago

0.0.34

5 months ago

0.0.20

5 months ago

0.0.21

5 months ago

0.0.23

5 months ago

0.0.24

5 months ago

0.0.25

5 months ago

0.0.15

5 months ago

0.0.16

5 months ago

0.0.17

5 months ago

0.0.18

5 months ago

0.0.10

5 months ago

0.0.11

5 months ago

0.0.12

5 months ago

0.0.13

5 months ago

0.0.14

5 months ago

0.0.26

5 months ago

0.0.9

5 months ago

0.0.27

5 months ago

0.0.28

5 months ago

0.0.29

5 months ago

0.0.8

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.3

5 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.2

5 months ago