@naturalcycles/js-lib 14.101.0 → 14.104.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.
Files changed (48) hide show
  1. package/dist/array/array.util.d.ts +15 -6
  2. package/dist/array/array.util.js +22 -8
  3. package/dist/decorators/memo.util.js +2 -2
  4. package/dist/index.d.ts +3 -2
  5. package/dist/index.js +1 -0
  6. package/dist/is.util.d.ts +40 -0
  7. package/dist/is.util.js +72 -0
  8. package/dist/json-schema/jsonSchemaBuilder.d.ts +3 -0
  9. package/dist/json-schema/jsonSchemaBuilder.js +7 -2
  10. package/dist/json-schema/jsonSchemas.js +4 -4
  11. package/dist/object/object.util.d.ts +0 -17
  12. package/dist/object/object.util.js +9 -52
  13. package/dist/object/sortObjectDeep.js +2 -2
  14. package/dist/types.d.ts +2 -0
  15. package/dist-esm/array/array.util.js +22 -8
  16. package/dist-esm/datetime/localTime.js +1 -2
  17. package/dist-esm/decorators/createPromiseDecorator.js +10 -5
  18. package/dist-esm/decorators/debounce.js +6 -1
  19. package/dist-esm/decorators/memo.util.js +1 -1
  20. package/dist-esm/error/assert.js +12 -3
  21. package/dist-esm/error/error.util.js +8 -7
  22. package/dist-esm/error/tryCatch.js +1 -1
  23. package/dist-esm/index.js +1 -0
  24. package/dist-esm/is.util.js +59 -0
  25. package/dist-esm/json-schema/jsonSchema.util.js +2 -3
  26. package/dist-esm/json-schema/jsonSchemaBuilder.js +8 -4
  27. package/dist-esm/json-schema/jsonSchemas.js +4 -4
  28. package/dist-esm/math/math.util.js +1 -1
  29. package/dist-esm/object/object.util.js +4 -44
  30. package/dist-esm/object/sortObjectDeep.js +1 -1
  31. package/dist-esm/promise/pMap.js +15 -27
  32. package/dist-esm/promise/pQueue.js +7 -2
  33. package/dist-esm/promise/pRetry.js +4 -1
  34. package/dist-esm/promise/pTimeout.js +4 -1
  35. package/dist-esm/string/json.util.js +1 -1
  36. package/dist-esm/string/safeJsonStringify.js +1 -1
  37. package/dist-esm/string/stringifyAny.js +1 -1
  38. package/dist-esm/vendor/is.js +7 -8
  39. package/package.json +1 -1
  40. package/src/array/array.util.ts +28 -14
  41. package/src/decorators/memo.util.ts +1 -1
  42. package/src/index.ts +6 -0
  43. package/src/is.util.ts +77 -0
  44. package/src/json-schema/jsonSchemaBuilder.ts +7 -2
  45. package/src/json-schema/jsonSchemas.ts +4 -4
  46. package/src/object/object.util.ts +1 -49
  47. package/src/object/sortObjectDeep.ts +1 -1
  48. package/src/types.ts +3 -0
package/src/is.util.ts ADDED
@@ -0,0 +1,77 @@
1
+ import { Primitive } from './typeFest'
2
+ import { AnyObject, FalsyValue, NullishValue } from './types'
3
+
4
+ type Nullish<T> = T extends NullishValue ? T : never
5
+ type Truthy<T> = T extends FalsyValue ? never : T
6
+ type Falsy<T> = T extends FalsyValue ? T : never
7
+
8
+ export const _isNull = <T>(v: T): v is T extends null ? T : never => v === null
9
+ export const _isUndefined = <T>(v: T): v is T extends undefined ? T : never =>
10
+ typeof v === 'undefined'
11
+ export const _isNullish = <T>(v: T): v is Nullish<T> => typeof v === 'undefined' || v === null
12
+ export const _isNotNullish = <T>(v: T): v is NonNullable<T> => v !== undefined && v !== null
13
+
14
+ /**
15
+ * Same as Boolean, but with correct type output.
16
+ * Related:
17
+ * https://github.com/microsoft/TypeScript/issues/16655
18
+ * https://www.karltarvas.com/2021/03/11/typescript-array-filter-boolean.html
19
+ *
20
+ * @example
21
+ *
22
+ * [1, 2, undefined].filter(_isTruthy)
23
+ * // => [1, 2]
24
+ */
25
+ export const _isTruthy = <T>(v: T): v is Truthy<T> => !!v
26
+ export const _isFalsy = <T>(v: T): v is Falsy<T> => !v
27
+
28
+ /**
29
+ * Returns true if item is Object, not null and not Array.
30
+ */
31
+ export function _isObject(item: any): item is AnyObject {
32
+ return (typeof item === 'object' && item !== null && !Array.isArray(item)) || false
33
+ }
34
+
35
+ export function _isPrimitive(v: any): v is Primitive {
36
+ return (
37
+ v === null ||
38
+ v === undefined ||
39
+ typeof v === 'number' ||
40
+ typeof v === 'boolean' ||
41
+ typeof v === 'string' ||
42
+ typeof v === 'bigint' ||
43
+ typeof v === 'symbol'
44
+ )
45
+ }
46
+
47
+ export function _isEmptyObject(obj: any): boolean {
48
+ return obj && obj.constructor === Object && Object.keys(obj).length === 0
49
+ }
50
+
51
+ /**
52
+ * Object is considered empty if it's one of:
53
+ * undefined
54
+ * null
55
+ * '' (empty string)
56
+ * [] (empty array)
57
+ * {} (empty object)
58
+ * new Map() (empty Map)
59
+ * new Set() (empty Set)
60
+ */
61
+ export function _isEmpty(obj: any): boolean {
62
+ if (obj === undefined || obj === null) return true
63
+
64
+ if (typeof obj === 'string' || Array.isArray(obj)) {
65
+ return obj.length === 0
66
+ }
67
+
68
+ if (obj instanceof Map || obj instanceof Set) {
69
+ return obj.size === 0
70
+ }
71
+
72
+ if (typeof obj === 'object') {
73
+ return Object.keys(obj).length === 0
74
+ }
75
+
76
+ return false
77
+ }
@@ -78,6 +78,9 @@ export const jsonSchema = {
78
78
  unixTimestamp() {
79
79
  return new JsonSchemaNumberBuilder().unixTimestamp()
80
80
  },
81
+ unixTimestamp2000() {
82
+ return new JsonSchemaNumberBuilder().unixTimestamp2000()
83
+ },
81
84
  // string types
82
85
  string() {
83
86
  return new JsonSchemaStringBuilder()
@@ -246,7 +249,9 @@ export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder<number, JsonSc
246
249
  float = () => this.format('float')
247
250
  double = () => this.format('double')
248
251
  unixTimestamp = () => this.format('unixTimestamp')
252
+ unixTimestamp2000 = () => this.format('unixTimestamp2000')
249
253
  unixTimestampMillis = () => this.format('unixTimestampMillis')
254
+ unixTimestampMillis2000 = () => this.format('unixTimestampMillis2000')
250
255
  utcOffset = () => this.format('utcOffset')
251
256
  utcOffsetHours = () => this.format('utcOffsetHours')
252
257
  }
@@ -370,8 +375,8 @@ export class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyB
370
375
  ): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>> {
371
376
  Object.assign(this.schema.properties, {
372
377
  id: { type: idType },
373
- created: { type: 'number', format: 'unixTimestamp' },
374
- updated: { type: 'number', format: 'unixTimestamp' },
378
+ created: { type: 'number', format: 'unixTimestamp2000' },
379
+ updated: { type: 'number', format: 'unixTimestamp2000' },
375
380
  })
376
381
 
377
382
  return this
@@ -3,12 +3,12 @@ import { jsonSchema } from './jsonSchemaBuilder'
3
3
 
4
4
  export const baseDBEntityJsonSchema = jsonSchema.object<BaseDBEntity>({
5
5
  id: jsonSchema.string().optional(),
6
- created: jsonSchema.unixTimestamp().optional(),
7
- updated: jsonSchema.unixTimestamp().optional(),
6
+ created: jsonSchema.unixTimestamp2000().optional(),
7
+ updated: jsonSchema.unixTimestamp2000().optional(),
8
8
  })
9
9
 
10
10
  export const savedDBEntityJsonSchema = jsonSchema.object<SavedDBEntity>({
11
11
  id: jsonSchema.string(),
12
- created: jsonSchema.unixTimestamp(),
13
- updated: jsonSchema.unixTimestamp(),
12
+ created: jsonSchema.unixTimestamp2000(),
13
+ updated: jsonSchema.unixTimestamp2000(),
14
14
  })
@@ -1,3 +1,4 @@
1
+ import { _isEmpty, _isObject } from '../is.util'
1
2
  import { PropertyPath } from '../lodash.types'
2
3
  import { AnyObject, ObjectMapper, ObjectPredicate, StringMap, ValueOf } from '../types'
3
4
 
@@ -190,55 +191,6 @@ export function _deepCopy<T>(o: T): T {
190
191
  return JSON.parse(JSON.stringify(o))
191
192
  }
192
193
 
193
- /**
194
- * Returns true if item is Object, not null and not Array.
195
- */
196
- export function _isObject(item: any): item is AnyObject {
197
- return (typeof item === 'object' && item !== null && !Array.isArray(item)) || false
198
- }
199
-
200
- export function _isPrimitive(v: any): v is null | undefined | number | boolean | string {
201
- return (
202
- v === null ||
203
- v === undefined ||
204
- typeof v === 'number' ||
205
- typeof v === 'boolean' ||
206
- typeof v === 'string'
207
- )
208
- }
209
-
210
- export function _isEmptyObject(obj: any): boolean {
211
- return obj && obj.constructor === Object && Object.keys(obj).length === 0
212
- }
213
-
214
- /**
215
- * Object is considered empty if it's one of:
216
- * undefined
217
- * null
218
- * '' (empty string)
219
- * [] (empty array)
220
- * {} (empty object)
221
- * new Map() (empty Map)
222
- * new Set() (empty Set)
223
- */
224
- export function _isEmpty(obj: any): boolean {
225
- if (obj === undefined || obj === null) return true
226
-
227
- if (typeof obj === 'string' || Array.isArray(obj)) {
228
- return obj.length === 0
229
- }
230
-
231
- if (obj instanceof Map || obj instanceof Set) {
232
- return obj.size === 0
233
- }
234
-
235
- if (typeof obj === 'object') {
236
- return Object.keys(obj).length === 0
237
- }
238
-
239
- return false
240
- }
241
-
242
194
  /**
243
195
  * Returns `undefined` if it's empty (according to `_isEmpty()` specification),
244
196
  * otherwise returns the original object.
@@ -1,4 +1,4 @@
1
- import { _isObject } from './object.util'
1
+ import { _isObject } from '..'
2
2
 
3
3
  /**
4
4
  * based on: https://github.com/IndigoUnited/js-deep-sort-object
package/src/types.ts CHANGED
@@ -260,3 +260,6 @@ export function _stringMapEntries<T>(m: StringMap<T>): [k: string, v: T][] {
260
260
  export function _objectKeys<T extends AnyObject>(obj: T): (keyof T)[] {
261
261
  return Object.keys(obj)
262
262
  }
263
+
264
+ export type NullishValue = null | undefined
265
+ export type FalsyValue = false | '' | 0 | null | undefined