forked from czabaj/stringify-replacers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
29 lines (24 loc) · 895 Bytes
/
utils.ts
File metadata and controls
29 lines (24 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Replacer } from "./types.d.ts";
export const { isArray } = Array;
// deno-lint-ignore no-explicit-any
export const isPlainObject = (value: any): value is Record<string, unknown> =>
value && typeof value === `object` && !isArray(value);
export const pipeSecondArg = (replacers: [Replacer, ...Replacer[]]): Replacer =>
// deno-lint-ignore no-explicit-any
function (this: any, k, v) {
return replacers.reduce(
(acc, fn) => fn.call(this, k, acc),
v,
);
};
export const pipeReplacers = (replacers: Replacer[]): Replacer | undefined =>
replacers.length === 0
? undefined
: replacers.length === 1
? replacers[0]
: pipeSecondArg(replacers as [Replacer, ...Replacer[]]);
/**
* Prints the type of object, must be invoked with Function#call
* e.g. printType.call([]) // => [object Array]
*/
export const printType = Object.prototype.toString;