https://github.com/blackglory/extra-utils
🌳 Utilities for JavaScript and Typescript
https://github.com/blackglory/extra-utils
browser esm library nodejs npm-package typescript
Last synced: 9 months ago
JSON representation
🌳 Utilities for JavaScript and Typescript
- Host: GitHub
- URL: https://github.com/blackglory/extra-utils
- Owner: BlackGlory
- License: mit
- Created: 2020-08-12T06:36:46.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-07T04:19:21.000Z (over 1 year ago)
- Last Synced: 2025-01-01T05:34:36.853Z (over 1 year ago)
- Topics: browser, esm, library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/extra-utils
- Size: 1.91 MB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# extra-utils
Utilities for JavaScript and Typescript.
## Install
```sh
npm install --save extra-utils
# or
yarn add extra-utils
```
## API
### ArrayLike
```ts
function first(arr: NonEmptyArray): T
function first(arr: ArrayLike): T | undefined
function last(arr: NonEmptyArray): T
function last(arr: ArrayLike): T | undefined
function ensureArray(value: Arrayable): T[]
```
### Array
```ts
function isArray(val: unknown): val is Array
function isntArray(val: T): val is Exclude>
function isEmptyArray(val: readonly unknown[]): boolean
function isntEmptyArray(val: readonly T[]): val is NonEmptyArray
```
### Boolean
```ts
function isBoolean(val: unknown): val is boolean
function isntBoolean(val: unknown): val is Exclude
function isFalsy(val: unknown): val is Falsy
function isntFalsy(val: T): val is Exclude
```
### JSON
```ts
function isJSONValue(val: unknown): val is JSONValue
function isntJSONValue(val: T): val is Exclude
function isJSONSerializable>
| Array>
>(val: unknown): val is JSONSerializable
function isntJSONSerializable(val: T): val is Exclude>
```
### Nullish
```ts
function isNullish(val: unknown): val is Nullish
function isntNullish(val: T): val is Exclude
function isNull(val: unknown): val is null
function isntNull(val: T): val is Exclude
function isUndefined(val: unknown): val is undefined
function isntUndefined(val: T): val is Exclude
```
### Number
```ts
function isNumber(val: unknown): val is number
function isntNumber(val: T): val is Exclude
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(val: T): val is Exclude
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(
value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray
): T
interface IWeightedItem {
weight: number
}
function remapToWeightedItem(
value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray
, weights: NonEmptyArray
): T
function remapToWeightedItem(
value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray
): T
function remapToIndex(
value: number
, range: readonly [min: number, max: number]
, items: NonEmptyArray
): number
function remapToWeightedIndex(
value: number
, range: [min: number, max: number]
, weights: NonEmptyArray
): number
function lerp(
alpha: number
, [from, to]: readonly [from: number, to: number]
): number
function modf(value: number): [integralPart: number, fractionalPart: number]
```
### Object
```ts
function isObject(
val: unknown
): val is object & Record
function isntObject(
val: T
): val is Exclude>
function isPlainObject(
val: unknown
): val is object & Record
function isntPlainObject(
val: T
): val is Exclude>
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(
entries: Iterable<[Key, Value]>
): Record
```
### String
```ts
function isString(val: unknown): val is string
function isntString(val: T): val is Exclude
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
```ts
function removeExtraIndents(
text: string
, options?: { ignoreBlankLines: boolean = false }
): string
```
Example:
```ts
removeExtraIndents(`
hello
world
`, { ignoreBlankLines: true })
// '\n'
// + 'hello' + '\n'
// + '\n'
// + 'world' + '\n'
```
#### removeBlankLines
```ts
function removeBlankLines(text: string): string
```
Example:
```ts
removeBlankLines(
'\n'
+ 'hello' + '\n'
+ '\n'
+ 'world' + '\n'
+ '\n'
)
// 'hello' + '\n'
// + 'world'
```
#### removeLeadingBlankLines
```ts
function removeLeadingBlankLines(
text: string
, maxRemovals: number = Infinity
): string
```
Example:
```ts
removeLeadingBlankLines(
' ' + '\n'
+ 'a' + '\n'
+ ' '
)
// 'a' + '\n'
// + ' '
```
#### removeTrailingBlankLines
```ts
function removeTrailingBlankLines(
text: string
, maxRemovals: number = Infinity
): string
```
Example:
```ts
removeTrailingBlankLines(
' ' + '\n'
+ 'a' + '\n'
+ ' '
)
// ' ' + '\n'
// + 'a'
```
### Enum
```ts
function inEnum>(
val: unknown
, _enum: T
): val is T[keyof T]
function notInEnum(
val: unknown
, _enum: Record
): boolean
function enumKeys>(
_enum: T
): Array
function enumValues>(
_enum: T
): Array
function enumEntries>(
_enum: T
): Array<{ [Key in keyof T]: [Key, T[Key]] }[keyof T]>
function getEnumKey>(
_enum: T
, enumValue: T[keyof T]
): keyof T
```
### Date
```ts
function isDate(val: unknown): val is Date
function isntDate(val: T): val is Exclude
```
### Function
```ts
function isFunction any>(
val: unknown
): val is T
function isntFunction(val: T): val is Exclude
```
### RegExp
```ts
function isRegExp(val: unknown): val is RegExp
function isntRegExp(val: T): val is Exclude
```
### Symbol
```ts
function isSymbol(val: unknown): val is symbol
function isntSymbol(val: T): val is Exclude
```
### Functional programming
#### not
```ts
function not(
predicate: (...args: Args) => boolean
): (...args: Args) => boolean
```
#### pipe
```ts
function pipe(
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(
value: A
, ...operators: [
(value: A) => B
, (value: B) => C
, (value: C) => D
, (value: D) => E
, (value: E) => F
, (value: F) => G
]
): G
function pipe(
value: A
, ...operators: [
(value: A) => B
, (value: B) => C
, (value: C) => D
, (value: D) => E
, (value: E) => F
]
): F
function pipe(
value: A
, ...operators: [
(value: A) => B
, (value: B) => C
, (value: C) => D
, (value: D) => E
]
): E
function pipe(
value: A
, ...operators: [
(value: A) => B
, (value: B) => C
, (value: C) => D
]
): D
function pipe(
value: A
, ...operators: [
(value: A) => B
, (value: B) => C
]
): C
function pipe(
value: A
, ...operators: [
(value: A) => B
]
): B
function pipe(
value: T
, ...operators: Array<(value: any) => unknown>
): U
```
#### pipeAsync
```ts
function pipeAsync(
value: Awaitable
, ...operators: [
(value: A) => Awaitable
, (value: B) => Awaitable
, (value: C) => Awaitable
, (value: D) => Awaitable
, (value: E) => Awaitable
, (value: F) => Awaitable
, (value: G) => Awaitable
]
): Promise
function pipeAsync(
value: Awaitable
, ...operators: [
(value: A) => Awaitable
, (value: B) => Awaitable
, (value: C) => Awaitable
, (value: D) => Awaitable
, (value: E) => Awaitable
, (value: F) => Awaitable
]
): Promise
function pipeAsync(
value: Awaitable
, ...operators: [
(value: A) => Awaitable
, (value: B) => Awaitable
, (value: C) => Awaitable
, (value: D) => Awaitable
, (value: E) => Awaitable
]
): Promise
function pipeAsync(
value: Awaitable
, ...operators: [
(value: A) => Awaitable
, (value: B) => Awaitable
, (value: C) => Awaitable
, (value: D) => Awaitable
]
): Promise
function pipeAsync(
value: Awaitable
, ...operators: [
(value: A) => Awaitable
, (value: B) => Awaitable
, (value: C) => Awaitable
]
): Promise
function pipeAsync(
value: Awaitable
, ...operators: [
(value: A) => Awaitable
, (value: B) => Awaitable
]
): Promise
function pipeAsync(
value: Awaitable
, ...operators: [
(value: A) => Awaitable
]
): Promise
function pipeAsync(
value: Awaitable
, ...operators: Array<(value: any) => Awaitable>
): Promise
```
#### Reducers
```ts
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
```