@hairy/utils 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -88,6 +88,7 @@ __export(index_exports, {
88
88
  isWeex: () => isWeex,
89
89
  isWindow: () => isWindow,
90
90
  join: () => join_default,
91
+ jsonTryParse: () => jsonTryParse,
91
92
  kebabCase: () => kebabCase,
92
93
  keys: () => keys_default,
93
94
  loop: () => loop,
@@ -2925,6 +2926,15 @@ var isMobile = () => isBrowser() && navigator.userAgent.toLowerCase().includes("
2925
2926
  var isFormData = (value) => isObject_default(value) && isBrowser() && value instanceof FormData;
2926
2927
  var isWindow = (value) => typeof window !== "undefined" && toString.call(value) === "[object Window]";
2927
2928
 
2929
+ // src/util/json.ts
2930
+ function jsonTryParse(text) {
2931
+ try {
2932
+ return JSON.parse(text || "");
2933
+ } catch {
2934
+ return void 0;
2935
+ }
2936
+ }
2937
+
2928
2938
  // src/util/noop.ts
2929
2939
  var noop2 = () => {
2930
2940
  };
@@ -3032,6 +3042,7 @@ function whenever(value, callback) {
3032
3042
  isWeex,
3033
3043
  isWindow,
3034
3044
  join,
3045
+ jsonTryParse,
3035
3046
  kebabCase,
3036
3047
  keys,
3037
3048
  loop,
@@ -0,0 +1,321 @@
1
+ export { clone, cloneDeep, concat, debounce, find, isArray, isArrayLike, isBoolean, isDate, isEmpty, isEqual, isError, isFunction, isNaN, isNative, isNull, isNumber, isObject, isPlainObject, isString, isUndefined, join, keys, truncate, uniq, uniqBy, values } from 'lodash-es';
2
+ import Bignumber from 'bignumber.js';
3
+ export { default as Bignumber } from 'bignumber.js';
4
+
5
+ /**
6
+ * Supported locale values. Use `false` to ignore locale.
7
+ * Defaults to `undefined`, which uses the host environment.
8
+ */
9
+ type Locale = string[] | string | false | undefined;
10
+ /**
11
+ * Options used for converting strings to pascal/camel case.
12
+ */
13
+ interface PascalCaseOptions extends Options {
14
+ mergeAmbiguousCharacters?: boolean;
15
+ }
16
+ /**
17
+ * Options used for converting strings to any case.
18
+ */
19
+ interface Options {
20
+ locale?: Locale;
21
+ split?: (value: string) => string[];
22
+ /** @deprecated Pass `split: splitSeparateNumbers` instead. */
23
+ separateNumbers?: boolean;
24
+ delimiter?: string;
25
+ prefixCharacters?: string;
26
+ suffixCharacters?: string;
27
+ }
28
+ /**
29
+ * Convert a string to space separated lower case (`foo bar`).
30
+ */
31
+ declare function noCase(input: string, options?: Options): string;
32
+ /**
33
+ * Convert a string to camel case (`fooBar`).
34
+ */
35
+ declare function camelCase(input: string, options?: PascalCaseOptions): string;
36
+ /**
37
+ * Convert a string to pascal case (`FooBar`).
38
+ */
39
+ declare function pascalCase(input: string, options?: PascalCaseOptions): string;
40
+ /**
41
+ * Convert a string to pascal snake case (`Foo_Bar`).
42
+ */
43
+ declare function pascalSnakeCase(input: string, options?: Options): string;
44
+ /**
45
+ * Convert a string to capital case (`Foo Bar`).
46
+ */
47
+ declare function capitalCase(input: string, options?: Options): string;
48
+ /**
49
+ * Convert a string to constant case (`FOO_BAR`).
50
+ */
51
+ declare function constantCase(input: string, options?: Options): string;
52
+ /**
53
+ * Convert a string to dot case (`foo.bar`).
54
+ */
55
+ declare function dotCase(input: string, options?: Options): string;
56
+ /**
57
+ * Convert a string to kebab case (`foo-bar`).
58
+ */
59
+ declare function kebabCase(input: string, options?: Options): string;
60
+ /**
61
+ * Convert a string to path case (`foo/bar`).
62
+ */
63
+ declare function pathCase(input: string, options?: Options): string;
64
+ /**
65
+ * Convert a string to path case (`Foo bar`).
66
+ */
67
+ declare function sentenceCase(input: string, options?: Options): string;
68
+ /**
69
+ * Convert a string to snake case (`foo_bar`).
70
+ */
71
+ declare function snakeCase(input: string, options?: Options): string;
72
+ /**
73
+ * Convert a string to header case (`Foo-Bar`).
74
+ */
75
+ declare function trainCase(input: string, options?: Options): string;
76
+
77
+ type Numeric = string | number | bigint;
78
+ interface DynamicObject {
79
+ [key: string]: any;
80
+ }
81
+ interface NumericObject {
82
+ [key: string]: Numeric;
83
+ }
84
+ interface StringObject {
85
+ [key: string]: string;
86
+ }
87
+ interface NumberObject {
88
+ [key: string]: string;
89
+ }
90
+ interface SymbolObject {
91
+ [key: string]: symbol;
92
+ }
93
+ type Key = string | number | symbol;
94
+ type BooleanLike = any;
95
+ type Noop = (...args: any[]) => any;
96
+
97
+ type DeepReadonly<T> = {
98
+ readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P];
99
+ };
100
+ type DeepRequired<T> = {
101
+ [P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
102
+ };
103
+ type DeepPartial<T> = {
104
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
105
+ };
106
+ type DeepReplace<T, K = unknown, V = unknown> = {
107
+ [P in keyof T]: K extends P ? V : DeepReplace<T[P], K, V>;
108
+ };
109
+ type DeepKeyof<T> = T extends object ? keyof T | DeepKeyof<T[keyof T]> : never;
110
+
111
+ type Awaitable<T> = T | Promise<T>;
112
+ type Arrayable<T> = T | T[];
113
+ type Option<L extends Key = 'label', V extends Key = 'value', C extends Key = 'children'> = {
114
+ [P in L]?: string;
115
+ } & {
116
+ [P in V]?: Numeric;
117
+ } & {
118
+ [P in C]?: Option<L, V, C>[];
119
+ };
120
+
121
+ declare const BIG_INTS: {
122
+ t: {
123
+ v: number;
124
+ d: number;
125
+ n: string;
126
+ };
127
+ b: {
128
+ v: number;
129
+ d: number;
130
+ n: string;
131
+ };
132
+ m: {
133
+ v: number;
134
+ d: number;
135
+ n: string;
136
+ };
137
+ k: {
138
+ v: number;
139
+ d: number;
140
+ n: string;
141
+ };
142
+ };
143
+ /**
144
+ * Any type that can be used where a big number is needed.
145
+ */
146
+ type Numberish = Numeric | {
147
+ toString: (...args: any[]) => string;
148
+ };
149
+ type Delimiter = 'k' | 'm' | 'b' | 't';
150
+ interface DecimalOptions {
151
+ d?: number;
152
+ r?: Bignumber.RoundingMode;
153
+ }
154
+ interface FormatGroupOptions {
155
+ size?: number;
156
+ symbol?: string;
157
+ }
158
+ interface FormatNumericOptions {
159
+ delimiters?: Delimiter[] | false;
160
+ rounding?: Bignumber.RoundingMode;
161
+ decimals?: number;
162
+ zeromove?: boolean;
163
+ format?: Bignumber.Format;
164
+ }
165
+ declare function unum(num?: Numeric): Bignumber;
166
+ declare function gte(num: Numeric, n: Numeric): boolean;
167
+ declare function gt(num: Numeric, n: Numeric): boolean;
168
+ declare function lte(num: Numeric, n: Numeric): boolean;
169
+ declare function lt(num: Numeric, n: Numeric): boolean;
170
+ declare function plus(array: Numeric[], options?: DecimalOptions): string;
171
+ declare function average(array: Numeric[], options?: DecimalOptions): string;
172
+ /**
173
+ * calculate percentage
174
+ * @param total
175
+ * @param count
176
+ */
177
+ declare function percentage(total: Numeric, count: Numeric, options?: DecimalOptions): string;
178
+ /**
179
+ * leading zeros
180
+ * @param value
181
+ * @param n
182
+ * @param type
183
+ */
184
+ declare function zerofill(value: Numberish, n?: number, type?: 'positive' | 'reverse'): Numberish;
185
+ declare function zeromove(value: Numberish): string;
186
+ declare function numerfix(value: any): string;
187
+ /**
188
+ * format as a positive integer
189
+ * @param value
190
+ */
191
+ declare function integer(value: Numberish): string;
192
+ /**
193
+ * retain n decimal places
194
+ * @param value
195
+ * @param n
196
+ */
197
+ declare function decimal(value: Numberish, n?: number): string;
198
+ declare function parseNumeric(num: Numeric, delimiters?: Delimiter[]): {
199
+ v: number;
200
+ d: number;
201
+ n: string;
202
+ };
203
+ /**
204
+ * format number thousand separator and unit
205
+ * @param value
206
+ * @param options
207
+ * @returns
208
+ */
209
+ declare function formatNumeric(value?: Numeric, options?: FormatNumericOptions): string;
210
+
211
+ type Dimension = Numeric | [Numeric, Numeric] | {
212
+ width: Numeric;
213
+ height: Numeric;
214
+ };
215
+ declare function formatUnit(value: Numeric, unit?: string): string;
216
+ declare function formatSize(dimension: Dimension, unit?: string): {
217
+ width: string;
218
+ height: string;
219
+ };
220
+
221
+ /**
222
+ * Intercept front and back characters, hide middle characters
223
+ * @param value
224
+ * @param mode
225
+ * @param symbol
226
+ */
227
+ declare function cover(value: string, mode: [number, number, number], symbol?: string): string;
228
+
229
+ type Typeof = 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function' | 'null' | 'regexp';
230
+ /**
231
+ * obtain data type
232
+ * @param target Detection object
233
+ */
234
+ declare function getTypeof(target: any): Typeof;
235
+ /**
236
+ * Detecting data types
237
+ * @param target Detection object
238
+ * @param type Data type
239
+ */
240
+ declare function isTypeof(target: any, type: Typeof): boolean;
241
+
242
+ declare const compose: (...fns: any[]) => any;
243
+
244
+ /**
245
+ * formData to object
246
+ * @param formData
247
+ */
248
+ declare function formToObject(formData: FormData): Record<string, string>;
249
+ /**
250
+ * Object to formData
251
+ * @param object
252
+ */
253
+ declare function objectToForm(object: Record<string, string | File>): FormData;
254
+
255
+ declare class Deferred<T> extends Promise<T> {
256
+ resolve: (value: T) => Deferred<T>;
257
+ reject: (reason?: any) => Deferred<T>;
258
+ constructor();
259
+ }
260
+
261
+ declare function delay(ms: number): Promise<void>;
262
+
263
+ declare const isBrowser: () => boolean;
264
+ declare const isWeex: () => boolean;
265
+ declare const isIE: () => boolean | "";
266
+ declare const isIE9: () => boolean | "";
267
+ declare const isIE11: () => boolean;
268
+ declare const isEdge: () => boolean | "";
269
+ declare const isAndroid: () => boolean;
270
+ declare const isIOS: () => boolean;
271
+ declare const isChrome: () => boolean | "";
272
+ declare const isPhantomJS: () => boolean | "";
273
+ declare const isFF: () => false | RegExpMatchArray | null;
274
+ declare const isMobile: () => boolean;
275
+ declare const isFormData: (value: any) => value is FormData;
276
+ declare const isWindow: (value: any) => value is Window;
277
+
278
+ declare function jsonTryParse(text: string | undefined | null): any;
279
+
280
+ declare const noop: (...args: any) => any;
281
+
282
+ type UnaryFunction<ValueType, ReturnType> = (value: ValueType) => ReturnType | PromiseLike<ReturnType>;
283
+ type Pipeline<ValueType, ReturnType> = (value?: ValueType) => Promise<ReturnType>;
284
+ /**
285
+ Compose promise-returning & async functions into a reusable pipeline.
286
+
287
+ @param ...input - Iterated over sequentially when returned `function` is called.
288
+ @returns The `input` functions are applied from left to right.
289
+
290
+ @example
291
+ ```
292
+ import { pPipe } from '@hairy/utils';
293
+
294
+ const addUnicorn = async string => `${string} Unicorn`;
295
+ const addRainbow = async string => `${string} Rainbow`;
296
+
297
+ const pipeline = pPipe(addUnicorn, addRainbow);
298
+
299
+ console.log(await pipeline('❤️'));
300
+ //=> '❤️ Unicorn Rainbow'
301
+ ```
302
+ */
303
+ declare function pPipe<ValueType, ReturnType>(f1: UnaryFunction<ValueType, ReturnType>): Pipeline<ValueType, ReturnType>;
304
+ declare function pPipe<ValueType, ResultValue1, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ReturnType>): Pipeline<ValueType, ReturnType>;
305
+ declare function pPipe<ValueType, ResultValue1, ResultValue2, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ResultValue2>, f3: UnaryFunction<ResultValue2, ReturnType>): Pipeline<ValueType, ReturnType>;
306
+ declare function pPipe<ValueType, ResultValue1, ResultValue2, ResultValue3, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ResultValue2>, f3: UnaryFunction<ResultValue2, ResultValue3>, f4: UnaryFunction<ResultValue3, ReturnType>): Pipeline<ValueType, ReturnType>;
307
+ declare function pPipe<ValueType, ResultValue1, ResultValue2, ResultValue3, ResultValue4, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ResultValue2>, f3: UnaryFunction<ResultValue2, ResultValue3>, f4: UnaryFunction<ResultValue3, ResultValue4>, f5: UnaryFunction<ResultValue4, ReturnType>): Pipeline<ValueType, ReturnType>;
308
+ declare function pPipe<ValueType, ResultValue1, ResultValue2, ResultValue3, ResultValue4, ResultValue5, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ResultValue2>, f3: UnaryFunction<ResultValue2, ResultValue3>, f4: UnaryFunction<ResultValue3, ResultValue4>, f5: UnaryFunction<ResultValue4, ResultValue5>, f6: UnaryFunction<ResultValue5, ReturnType>): Pipeline<ValueType, ReturnType>;
309
+ declare function pPipe<ValueType, ResultValue1, ResultValue2, ResultValue3, ResultValue4, ResultValue5, ResultValue6, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ResultValue2>, f3: UnaryFunction<ResultValue2, ResultValue3>, f4: UnaryFunction<ResultValue3, ResultValue4>, f5: UnaryFunction<ResultValue4, ResultValue5>, f6: UnaryFunction<ResultValue5, ResultValue6>, f7: UnaryFunction<ResultValue6, ReturnType>): Pipeline<ValueType, ReturnType>;
310
+ declare function pPipe<ValueType, ResultValue1, ResultValue2, ResultValue3, ResultValue4, ResultValue5, ResultValue6, ResultValue7, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ResultValue2>, f3: UnaryFunction<ResultValue2, ResultValue3>, f4: UnaryFunction<ResultValue3, ResultValue4>, f5: UnaryFunction<ResultValue4, ResultValue5>, f6: UnaryFunction<ResultValue5, ResultValue6>, f7: UnaryFunction<ResultValue6, ResultValue7>, f8: UnaryFunction<ResultValue7, ReturnType>): Pipeline<ValueType, ReturnType>;
311
+ declare function pPipe<ValueType, ResultValue1, ResultValue2, ResultValue3, ResultValue4, ResultValue5, ResultValue6, ResultValue7, ResultValue8, ReturnType>(f1: UnaryFunction<ValueType, ResultValue1>, f2: UnaryFunction<ResultValue1, ResultValue2>, f3: UnaryFunction<ResultValue2, ResultValue3>, f4: UnaryFunction<ResultValue3, ResultValue4>, f5: UnaryFunction<ResultValue4, ResultValue5>, f6: UnaryFunction<ResultValue5, ResultValue6>, f7: UnaryFunction<ResultValue6, ResultValue7>, f8: UnaryFunction<ResultValue7, ResultValue8>, f9: UnaryFunction<ResultValue8, ReturnType>): Pipeline<ValueType, ReturnType>;
312
+
313
+ declare const pipe: (...fns: any[]) => any;
314
+
315
+ declare function arange(x1: number, x2?: number, stp?: number, z?: number[], z0?: number): number[];
316
+ declare function loop<T = void>(fn: (next: (ms: number) => Promise<T>) => Promise<T>): Promise<T>;
317
+ declare function riposte<T>(...args: [cond: boolean, value: T][]): T;
318
+ declare function unwrap<T extends object>(value: T | (() => T)): T;
319
+ declare function whenever<T, C extends (value: Exclude<T, null | undefined>) => any>(value: T, callback: C): ReturnType<C> | undefined;
320
+
321
+ export { type Arrayable, type Awaitable, BIG_INTS, type BooleanLike, type DecimalOptions, type DeepKeyof, type DeepPartial, type DeepReadonly, type DeepReplace, type DeepRequired, Deferred, type Delimiter, type Dimension, type DynamicObject, type FormatGroupOptions, type FormatNumericOptions, type Key, type Noop, type NumberObject, type Numberish, type Numeric, type NumericObject, type Option, type Pipeline, type StringObject, type SymbolObject, type Typeof, type UnaryFunction, arange, average, camelCase, capitalCase, compose, constantCase, cover, decimal, delay, dotCase, formToObject, formatNumeric, formatSize, formatUnit, getTypeof, gt, gte, integer, isAndroid, isBrowser, isChrome, isEdge, isFF, isFormData, isIE, isIE11, isIE9, isIOS, isMobile, isPhantomJS, isTypeof, isWeex, isWindow, jsonTryParse, kebabCase, loop, lt, lte, noCase, noop, numerfix, objectToForm, pPipe, parseNumeric, pascalCase, pascalSnakeCase, pathCase, percentage, pipe, plus, riposte, sentenceCase, snakeCase, trainCase, unum, unwrap, whenever, zerofill, zeromove };
@@ -4147,6 +4147,15 @@
4147
4147
  var isFormData = (value) => isObject_default(value) && isBrowser() && value instanceof FormData;
4148
4148
  var isWindow = (value) => typeof window !== "undefined" && toString.call(value) === "[object Window]";
4149
4149
 
4150
+ // src/util/json.ts
4151
+ function jsonTryParse(text) {
4152
+ try {
4153
+ return JSON.parse(text || "");
4154
+ } catch {
4155
+ return void 0;
4156
+ }
4157
+ }
4158
+
4150
4159
  // src/util/noop.ts
4151
4160
  var noop2 = () => {
4152
4161
  };
package/dist/index.js CHANGED
@@ -2802,6 +2802,15 @@ var isMobile = () => isBrowser() && navigator.userAgent.toLowerCase().includes("
2802
2802
  var isFormData = (value) => isObject_default(value) && isBrowser() && value instanceof FormData;
2803
2803
  var isWindow = (value) => typeof window !== "undefined" && toString.call(value) === "[object Window]";
2804
2804
 
2805
+ // src/util/json.ts
2806
+ function jsonTryParse(text) {
2807
+ try {
2808
+ return JSON.parse(text || "");
2809
+ } catch {
2810
+ return void 0;
2811
+ }
2812
+ }
2813
+
2805
2814
  // src/util/noop.ts
2806
2815
  var noop2 = () => {
2807
2816
  };
@@ -2908,6 +2917,7 @@ export {
2908
2917
  isWeex,
2909
2918
  isWindow,
2910
2919
  join_default as join,
2920
+ jsonTryParse,
2911
2921
  kebabCase,
2912
2922
  keys_default as keys,
2913
2923
  loop,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/utils",
3
3
  "type": "module",
4
- "version": "1.6.1",
4
+ "version": "1.7.0",
5
5
  "description": "Library for anywhere",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",