2.3.1 • Published 4 months ago

@whoj/utils-core v2.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@whoj/utils-core

Install

# ✨ Auto-detect
npx nypm install @whoj/utils-core

# npm
npm install @whoj/utils-core

# yarn
yarn add @whoj/utils-core

# pnpm
pnpm install @whoj/utils-core

# bun
bun install @whoj/utils-core

# deno
deno install @whoj/utils-core

Available Functions

Array

at(array, index)

clampArrayRange(n, arr)

Clamp a number to the index ranage of an array

flattenArrayable(array?)

Convert Arrayable<T> to Array<T> and flatten it

flattenDeepArray(array?, key?)

Convert Arrayable<T> to Array<T> and flatten object's array by key

Example: const payload = [ { child: {name: 'John Doe'} }, { child: {age: 23} } ]; flattenDeepArray(payload, 'child'); Result: {name: 'John Doe'}, {age: 23}

last(array)

mergeArrayable()

Use rest arguments to merge arrays

move(arr, from, to)

Move element in an Array

partition(array)

range()

remove(array, value)

Remove an item from Array

sample(arr, count)

Get random items from an array

shuffle(array)

Shuffle an array. This function mutates the array.

toArray(array?)

Convert Arrayable<T> to Array<T>

uniq(array)

Unique an Array

Function

batchInvoke(functions)

Call every function in an array

invoke(fn)

Call the function

tap(val, callback)

Pass the value through the callback, and return the value

Example:

function createUser(name: string): User {
  return tap(new User, user => {
    user.name = name
  })
}

Globals

getDocument()

If document is defined, return document, otherwise return undefined.

getGlobal(prop?)

If we're on the server, return the global object, otherwise return the window object

getWindow()

If we're on the server, return undefined, otherwise return the window object.

isClient()

If the window object exists, then we're in the browser, otherwise we're in Node.

isServer()

If the type of window is undefined, then we're on the server.

scrollToElement(element)

Guard

isTruthy(v)

Type guard to filter out falsy values

Example:

array.filter(isTruthy)

noNull(v)

Type guard to filter out null values

Example:

array.filter(noNull)

notNullish(v)

Type guard to filter out null-ish values

Example:

array.filter(notNullish)

notUndefined(v)

Type guard to filter out null-ish values

Example:

array.filter(notUndefined)

Math

clamp(n, min, max)

Clamp any number

sum()

Sum any amount of number

Object

clearUndefined(obj)

Clear undefined fields from an object. It mutates the object

deepMerge(target)

Deep merge :P

fromPairs(pairs)

isKeyOf(obj, k)

Type guard for any key, k. Marks k as a key of T if k is in obj.

objectEntries(obj)

Strict typed Object.entries

objectKeys(obj)

Strict typed Object.keys

objectMap(obj, fn)

Map key/value pairs for an object, and construct a new one

Example:

objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()])
// { A: '1', B: '2' }

Swap key/value:

Example:

objectMap({ a: 1, b: 2 }, (k, v) => [v, k])
// { 1: 'a', 2: 'b' }

Filter keys:

Example:

objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v])
// { b: 2 }

objectPick(obj, keys, omitUndefined)

Create a new subset object by giving keys

toPairs(obj)

Promise

createControlledPromise()

Return a Promise with resolve and reject methods

Example:

const promise = createControlledPromise()

await promise

// in anther context:
promise.resolve(data)

createPromiseLock()

Create a promise lock

Example:

const lock = createPromiseLock()

lock.run(async () => {
  await doSomething()
})

// in anther context:
await lock.wait() // it will wait all tasking finished

createSingletonPromise(fn)

Create singleton promise function

parallel(tasks, fn)

Parallel Promise

parallelSafe(tasks, fn)

Safe Parallel Promise

serial(tasks, fn)

Serial Promise

sleep(ms, callback?)

Promised setTimeout

String

capitalize()

divideStr(str, separator, position)

ensurePrefix(prefix, str)

Ensure prefix of a string

ensureSuffix(suffix, str)

Ensure suffix of a string

slash(str)

Replace backslash to slash

template(str)

Dead simple template engine, just like Python's .format()

Example:

const result = template(
  'Hello {0}! My name is {1}.',
  'John',
  'B.'
) // Hello John! My name is B..

Timer

stoppableTimeOut(cb, interval)

timestamp()

Timestamp value

Others

assert(condition, message)

Assert

noop()

Noop

hasConsole

  • Type: boolean
  • Default: true

isArray(val)

Check if is Array

isBoolean(val)

Check if value is boolean

isBrowser

  • Type: boolean
  • Default: false

isDef(val?)

isFunction(val)

Check if value is Function

isGlobPattern(pattern?, options?)

Check if it has glob pattern

isNumber(val)

Check if value is Number

isNumberish(val)

Check if value is Numberish

isObject(val)

Check if value is Object

isString(val)

Check if value is String

isWindow(val)

Check if window defined

MIT License © 2022 Jonson B.

2.3.0

4 months ago

2.2.0

4 months ago

2.3.1

4 months ago

2.1.1

5 months ago

2.1.0

6 months ago

2.0.2

6 months ago

2.0.1

7 months ago

2.0.0

8 months ago

2.0.0-beta.2

9 months ago

2.0.0-beta.1

9 months ago

1.8.0

2 years ago

1.7.5

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.6.2

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.7.4

2 years ago

1.5.0

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago