パッケージの詳細

extra-utils

BlackGlory24.8kMIT5.19.0

Utilities for JavaScript and Typescript

readme

extra-utils

Utilities for JavaScript and Typescript.

Install

npm install --save extra-utils
# or
yarn add extra-utils

API

ArrayLike

function first<T>(arr: NonEmptyArray<T>): T
function first<T>(arr: ArrayLike<T>): T | undefined

function last<T>(arr: NonEmptyArray<T>): T
function last<T>(arr: ArrayLike<T>): T | undefined

function ensureArray<T>(value: Arrayable<T>): T[]

Array

function isArray<T>(val: unknown): val is Array<T>
function isntArray<T>(val: T): val is Exclude<T, Array<unknown>>

function isEmptyArray(val: readonly unknown[]): boolean
function isntEmptyArray<T>(val: readonly T[]): val is NonEmptyArray<T>

Boolean

function isBoolean(val: unknown): val is boolean
function isntBoolean<T>(val: unknown): val is Exclude<T, boolean>

function isFalsy(val: unknown): val is Falsy
function isntFalsy<T>(val: T): val is Exclude<T, Falsy>

JSON

function isJSONValue(val: unknown): val is JSONValue
function isntJSONValue<T>(val: T): val is Exclude<T, JSONValue>

function isJSONSerializable<T extends
| JSONValue
| Record<string, JSONValue | JSONSerializable<any>>
| Array<JSONValue | JSONSerializable<any>>
>(val: unknown): val is JSONSerializable<T>
function isntJSONSerializable<T>(val: T): val is Exclude<T, JSONSerializable<any>>

Nullish

function isNullish(val: unknown): val is Nullish
function isntNullish<T>(val: T): val is Exclude<T, Nullish>

function isNull(val: unknown): val is null
function isntNull<T>(val: T): val is Exclude<T, null>

function isUndefined(val: unknown): val is undefined
function isntUndefined<T>(val: T): val is Exclude<T, undefined>

Number

function isNumber(val: unknown): val is number
function isntNumber<T>(val: T): val is Exclude<T, number>

function isFinite(val: number): boolean
function isPositiveInfinity(val: number): boolean
function isNegativeInfinity(val: number): boolean

function isNaN(val: number): boolean
function isntNaN(val: number): boolean

function isBigInt(val: unknown): val is bigint
function isntBigInt<T>(val: T): val is Exclude<T, bigint>

function clamp(
  val: number
, [min, max]: readonly [min: number, max: number]
): number

function remap(
  value: number
, [oldMin, oldMax]: readonly [oldMin: number, oldMax: number]
, [newMin, newMax]: readonly [newMin: number, newMax: number]
): number

function remapToItem<T>(
  value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray<T>
): T

interface IWeightedItem {
  weight: number
}

function remapToWeightedItem<T>(
  value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray<T>
, weights: NonEmptyArray<number>
): T
function remapToWeightedItem<T extends IWeightedItem>(
  value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray<T>
): T

function remapToIndex(
  value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray<unknown>
): number

function remapToWeightedIndex(
  value: number
, range: [min: number, max: number]
, weights: NonEmptyArray<number>
): number

function lerp(
  alpha: number
, [from, to]: readonly [from: number, to: number]
): number

function modf(value: number): [integralPart: number, fractionalPart: number]

Object

function isObject(
  val: unknown
): val is object & Record<string | symbol | number, unknown>
function isntObject<T>(
  val: T
): val is Exclude<T, object & Record<string | symbol | number, unknown>>

function isPlainObject(
  val: unknown
): val is object & Record<string | symbol | number, unknown>
function isntPlainObject<T>(
  val: T
): val is Exclude<T, object & Record<string | symbol | number, unknown>>

function isEmptyObject(val: object): boolean
function isntEmptyObject(val: object): boolean

function isReferenceEqual(a: unknown, b: unknown): boolean
function isShallowEqual(a: unknown, b: unknown): boolean
function isDeepEqual(a: unknown, b: unknown): boolean

function fromEntries<Key extends number | string, Value>(
  entries: Iterable<[Key, Value]>
): Record<Key, Value>

String

function isString(val: unknown): val is string
function isntString<T>(val: T): val is Exclude<T, string>

function isChar(val: unknown): val is string
function isntChar(val: unknown): boolean

function isURLString(text: string): boolean
function isntURLString(text: string): boolean

function toString(val: unknown): string

removeExtraIndents

function removeExtraIndents(
  text: string
, options?: { ignoreBlankLines: boolean = false }
): string

Example:

removeExtraIndents(`
  hello

  world
`, { ignoreBlankLines: true })
//   '\n'
// + 'hello' + '\n'
// + '\n'
// + 'world' + '\n'

removeBlankLines

function removeBlankLines(text: string): string

Example:

removeBlankLines(
  '\n'
+ 'hello' + '\n'
+ '\n'
+ 'world' + '\n'
+ '\n'
)
//   'hello' + '\n'
// + 'world'

removeLeadingBlankLines

function removeLeadingBlankLines(
  text: string
, maxRemovals: number = Infinity
): string

Example:

removeLeadingBlankLines(
  '  ' + '\n'
+ 'a' + '\n'
+ '  '
)
//   'a' + '\n'
// + '  '

removeTrailingBlankLines

function removeTrailingBlankLines(
  text: string
, maxRemovals: number = Infinity
): string

Example:

removeTrailingBlankLines(
  '  ' + '\n'
+ 'a' + '\n'
+ '  '
)
//   '  ' + '\n'
// + 'a'

Enum

function inEnum<T extends Record<string, string | number>>(
  val: unknown
, _enum: T
): val is T[keyof T]
function notInEnum(
  val: unknown
, _enum: Record<string, string | number>
): boolean

function enumKeys<T extends Record<string, string | number>>(
  _enum: T
): Array<keyof T>
function enumValues<T extends Record<string, string | number>>(
  _enum: T
): Array<T[keyof T]>
function enumEntries<T extends Record<string, string | number>>(
  _enum: T
): Array<{ [Key in keyof T]: [Key, T[Key]] }[keyof T]>

function getEnumKey<T extends Record<string, string | number>>(
  _enum: T
, enumValue: T[keyof T]
): keyof T

Date

function isDate(val: unknown): val is Date
function isntDate<T>(val: T): val is Exclude<T, Date>

Function

function isFunction<T extends Function = (...args: any[]) => any>(
  val: unknown
): val is T
function isntFunction<T>(val: T): val is Exclude<T, Function>

RegExp

function isRegExp(val: unknown): val is RegExp
function isntRegExp<T>(val: T): val is Exclude<T, RegExp>

Symbol

function isSymbol(val: unknown): val is symbol
function isntSymbol<T>(val: T): val is Exclude<T, symbol>

Functional programming

not

function not<Args extends unknown[]>(
  predicate: (...args: Args) => boolean
): (...args: Args) => boolean

pipe

function pipe<A, B, C, D, E, F, G, H>(
  value: A
, ...operators: [
    (value: A) => B
  , (value: B) => C
  , (value: C) => D
  , (value: D) => E
  , (value: E) => F
  , (value: F) => G
  , (value: G) => H
  ]
): H
function pipe<A, B, C, D, E, F, G>(
  value: A
, ...operators: [
    (value: A) => B
  , (value: B) => C
  , (value: C) => D
  , (value: D) => E
  , (value: E) => F
  , (value: F) => G
  ]
): G
function pipe<A, B, C, D, E, F>(
  value: A
, ...operators: [
    (value: A) => B
  , (value: B) => C
  , (value: C) => D
  , (value: D) => E
  , (value: E) => F
  ]
): F
function pipe<A, B, C, D, E>(
  value: A
, ...operators: [
    (value: A) => B
  , (value: B) => C
  , (value: C) => D
  , (value: D) => E
  ]
): E
function pipe<A, B, C, D>(
  value: A
, ...operators: [
    (value: A) => B
  , (value: B) => C
  , (value: C) => D
  ]
): D
function pipe<A, B, C>(
  value: A
, ...operators: [
    (value: A) => B
  , (value: B) => C
  ]
): C
function pipe<A, B>(
  value: A
, ...operators: [
    (value: A) => B
  ]
): B
function pipe<T, U>(
  value: T
, ...operators: Array<(value: any) => unknown>
): U

pipeAsync

function pipeAsync<A, B, C, D, E, F, G, H>(
  value: Awaitable<A>
, ...operators: [
    (value: A) => Awaitable<B>
  , (value: B) => Awaitable<C>
  , (value: C) => Awaitable<D>
  , (value: D) => Awaitable<E>
  , (value: E) => Awaitable<F>
  , (value: F) => Awaitable<G>
  , (value: G) => Awaitable<H>
  ]
): Promise<H>
function pipeAsync<A, B, C, D, E, F, G>(
  value: Awaitable<A>
, ...operators: [
    (value: A) => Awaitable<B>
  , (value: B) => Awaitable<C>
  , (value: C) => Awaitable<D>
  , (value: D) => Awaitable<E>
  , (value: E) => Awaitable<F>
  , (value: F) => Awaitable<G>
  ]
): Promise<G>
function pipeAsync<A, B, C, D, E, F>(
  value: Awaitable<A>
, ...operators: [
    (value: A) => Awaitable<B>
  , (value: B) => Awaitable<C>
  , (value: C) => Awaitable<D>
  , (value: D) => Awaitable<E>
  , (value: E) => Awaitable<F>
  ]
): Promise<F>
function pipeAsync<A, B, C, D, E>(
  value: Awaitable<A>
, ...operators: [
    (value: A) => Awaitable<B>
  , (value: B) => Awaitable<C>
  , (value: C) => Awaitable<D>
  , (value: D) => Awaitable<E>
  ]
): Promise<E>
function pipeAsync<A, B, C, D>(
  value: Awaitable<A>
, ...operators: [
    (value: A) => Awaitable<B>
  , (value: B) => Awaitable<C>
  , (value: C) => Awaitable<D>
  ]
): Promise<D>
function pipeAsync<A, B, C>(
  value: Awaitable<A>
, ...operators: [
    (value: A) => Awaitable<B>
  , (value: B) => Awaitable<C>
  ]
): Promise<C>
function pipeAsync<A, B>(
  value: Awaitable<A>
, ...operators: [
    (value: A) => Awaitable<B>
  ]
): Promise<B>
function pipeAsync<T, U>(
  value: Awaitable<T>
, ...operators: Array<(value: any) => Awaitable<unknown>>
): Promise<U>

Reducers

function max(result: number, current: number): number
function min(result: number, current: number): number
function sum(result: number, current: number): number
function product(result: number, current: number): number

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.19.0 (2025-03-09)

Features

5.18.1 (2025-02-13)

Bug Fixes

  • remap-to-weighted-index: an edge case caused by float precision (7c88436)

5.18.0 (2025-02-06)

Features

5.17.0 (2024-12-07)

Features

5.16.0 (2024-09-14)

Features

  • add remapToItem, remapToWeightedItem (8a540e9)

5.15.0 (2024-09-14)

Features

  • add remapToIndex, remapToWeightedIndex (6c1013e)

5.14.0 (2024-09-13)

Features

5.13.0 (2024-09-03)

Features

5.12.0 (2024-09-03)

Features

5.11.0 (2024-08-10)

Features

5.10.0 (2024-07-19)

Features

5.9.0 (2024-05-20)

Features

5.8.0 (2024-04-17)

Features

5.7.0 (2024-03-08)

Features

5.6.0 (2023-12-22)

Features

5.5.2 (2023-10-20)

5.5.1 (2023-10-16)

5.5.0 (2023-10-12)

Features

  • add enumKeys, enumValues, enumEntries (fd6a8c9)

Bug Fixes

  • edge cases of inEnum, notInEnum (15b953f)

5.4.0 (2023-07-20)

Features

  • add isReferenceEqual, isShallowEqual, isDeepEqual (0fc8a5f)

5.3.0 (2023-06-14)

Features

5.2.0 (2023-04-17)

Features

5.1.1 (2023-03-25)

5.1.0 (2023-02-18)

Features

5.0.1 (2023-01-22)

5.0.0 (2023-01-21)

⚠ BREAKING CHANGES

  • CommonJS => ESM

  • commonjs => esm (874df2f)

4.0.1 (2023-01-21)

4.0.0 (2022-12-19)

⚠ BREAKING CHANGES

    • Renamed isJson to isJSONValue
  • Renamed isntJson to isntJSONValue
  • Renamed isJsonable to isJSONSerializable
  • Renamed isntJsonable to isntJSONSerializable

  • upgrade dependencies (3cd7ea5)

3.7.0 (2022-12-14)

Features

  • add maxRemovals parameters (9f7b5a4)

3.6.0 (2022-12-14)

Features

  • add removeExtraIndents, removeBlankLines, removeLeadingBlankLines, removeTrailingBlankLines (6ab15c7)

3.5.1 (2022-11-25)

Bug Fixes

3.5.0 (2022-11-24)

Features

3.4.0 (2022-11-24)

Features

  • add isJsonable, isntJsonable (0d3a616)

3.3.1 (2022-11-17)

3.3.0 (2022-11-07)

Features

3.2.0 (2022-11-07)

Features

3.1.0 (2022-11-07)

Features

3.0.0 (2022-11-07)

⚠ BREAKING CHANGES

    • Renamed isURL to isURLString

Features

  • add isntURLString, notInEnum (71e3bc5)

2.0.0 (2022-11-07)

⚠ BREAKING CHANGES

  • Removed isIterable, isAsyncIterable, isntIterable, isntAsyncIterable
  • Removed isJsonable, isntJsonable
  • Renamed isAbsoluteURL to isURL
  • Removed isPromise, isPromiseLike, isntPromise, isntPromiseLike
  • Removed ES2015 support

Features

  • remove isIterable, isAsyncIterable, isntIterable, isntAsyncIterable (47a2923)
  • remove isJsonable, isntJsonable (35e2d5e)
  • remove isPromise, isPromiseLike, isntPromise, isntPromiseLike (a3362f7)
  • remove ES2015 support (c13bca6)
  • rename isAbsoluteURL to isURL (757839e)

1.4.0 (2022-10-30)

Features

  • add isNullish, isntNullish (d55d412)

1.3.0 (2022-08-23)

Features

  • add isSymbol, isntSymbol (e1e8bbb)

1.2.1 (2022-07-22)

1.2.0 (2022-06-21)

Features

  • add isRegExp, isntRegExp (d1cfc5b)

1.1.0 (2022-04-07)

Features

  • add isFinity, isPositiveInfinity, isNegativeInfinity, isNaN, isntNaN (d0f2fc5)

1.0.1 (2022-03-23)

1.0.0 (2022-03-05)

⚠ BREAKING CHANGES

  • remove isJsonRpcNotification, isntJsonRpcNotification,
       isJsonRpcRequest, isntJsonRpcRequest, isJsonRpcSuccess,
       isntJsonRpcSuccess, isJsonRpcError, isntJsonRpcError

Features

  • add isPromise, isntPromise, isPromiseLike, isntPromiseLike (810f5ae)
  • rewrite isJson, isntJson, add isJsonable, isntJsonable (35a2242)

0.6.5 (2022-01-05)

0.6.4 (2021-12-17)

Features

0.6.3 (2021-12-16)

Features

  • improve type assertions (6043e3d)

0.6.2 (2021-12-16)

0.6.1 (2021-12-16)

Features

  • replace object with Record (ee749a9)

0.6.0 (2021-12-15)

⚠ BREAKING CHANGES

    • isRecord removed.
  • The signatures of isEmptyObject, isntEmptyObject changed.

Features

  • add isPlainObject, isntPlainObject (13ce952)

0.5.2 (2021-10-19)

Features

  • improve isJsonRpcSuccess (2fe1247)

0.5.1 (2021-09-22)

Features

0.5.0 (2021-08-28)

⚠ BREAKING CHANGES

    • isCharPrimitive is removed
  • isntCharPrimitive is removed
  • isCharObject is removed
  • isntCharObject
  • isStringPrimitive is removed
  • isntStringPrimitive is removed
  • isStringObject is removed
  • isntStringObject is removed

Features

  • remove unused functions (5bd4704)

0.4.7 (2021-07-10)

Features

  • add isBigInt, isntBigInt (1c66209)

0.4.6 (2021-07-03)

0.4.5 (2021-06-30)

Bug Fixes

0.4.4 (2021-06-30)

Features

  • add inEnum (c1a0736)
  • add isEmptyArray, isntEmptyArray (a47d6f8)
  • add isEmptyObject, isntEmptyObject (9c430ad)

0.4.3 (2021-06-04)

Features

Bug Fixes

0.4.2 (2021-04-04)

Features

  • add isBoolean, isntBoolean (c8740aa)

0.4.1 (2021-03-17)

0.4.0 (2021-03-17)

⚠ BREAKING CHANGES

  • rewrite

Features

0.3.3 (2021-03-08)

Features

0.3.2 (2021-03-08)

Features

  • add UnboxPromise, UnboxPromiseLike (36ba941)

0.3.1 (2021-03-06)

Features

0.3.0 (2021-03-05)

⚠ BREAKING CHANGES

  • remove isPromise, isPromiseLike, isntPromise, isntPromiseLike,
     isDocument, isElement, isntDocument, isntElement,
     isNodeJSWritableStream, isNodeReadableStream,
     isntNodeJSWritableStream, isntNodeJSReadableStream

Features

  • move functions to extra-dom, extra-promise, extra-stream (6e5b169)

0.2.25 (2021-03-05)

0.2.24 (2021-03-05)

0.2.23 (2021-02-28)

Features

0.2.22 (2021-02-28)

Features

  • add Constructor, ReturnTypeOfConstructor (d49b0d1)

0.2.21 (2021-02-28)

Bug Fixes

0.2.20 (2021-02-28)

Features

0.2.19 (2021-02-28)

Features

  • add array, object interfaces (d91e3b1)

0.2.18 (2021-02-25)

0.2.17 (2021-02-24)

0.2.16 (2021-02-21)

Features

0.2.15 (2021-02-10)

Bug Fixes

0.2.14 (2021-02-04)

Bug Fixes

0.2.13 (2021-02-04)

0.2.12 (2021-02-03)

0.2.11 (2021-01-31)

0.2.10 (2021-01-31)

Features

0.2.9 (2021-01-20)

Bug Fixes

0.2.8 (2021-01-20)

0.2.7 (2021-01-16)

Features

  • add isElement, isDocument (e9cb820)

0.2.6 (2021-01-15)

Bug Fixes

0.2.5 (2021-01-11)

Features

0.2.4 (2021-01-04)

Bug Fixes

0.2.3 (2021-01-04)

0.2.2 (2021-01-04)

Bug Fixes

0.2.1 (2020-12-26)

Features

0.2.0 (2020-12-24)

⚠ BREAKING CHANGES

  • use instanceof for HTMLElement

Features

  • remove type guards for HTMLElement (0a89641)

0.1.12 (2020-12-24)

Features

  • add HTMLElement type guards (6f33d30)

0.1.11 (2020-12-20)

0.1.10 (2020-12-19)

Features

0.1.9 (2020-12-14)

Features

0.1.8 (2020-12-04)

Features

0.1.7 (2020-11-30)

Features

0.1.6 (2020-11-21)

0.1.5 (2020-11-21)

0.1.4 (2020-11-21)

Features

  • add isNodeJSWritableStream, isNodeJSReadableStream (2e39b80)