@onivoro/isomorphic-common 24.30.12 → 24.30.13

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/README.md CHANGED
@@ -11,7 +11,7 @@ npm install @onivoro/isomorphic-common
11
11
  ## Features
12
12
 
13
13
  - **Isomorphic Design**: Works identically in browser and Node.js environments
14
- - **String Manipulation**: Case conversion, formatting, and validation utilities
14
+ - **String Manipulation**: Case conversion, formatting, sanitization, and validation utilities
15
15
  - **Array Operations**: Chunking, sorting, deduplication, and transformation functions
16
16
  - **Date/Time Utilities**: Calendar operations, offset calculations, and time constants
17
17
  - **Financial Functions**: Currency formatting and money calculations
@@ -19,17 +19,23 @@ npm install @onivoro/isomorphic-common
19
19
  - **Validation Functions**: Data validation and parsing utilities
20
20
  - **Testing Helpers**: Mock functions and test arrangement utilities
21
21
  - **Constants**: Authentication headers, regex patterns, and time constants
22
+ - **Object Utilities**: Property extraction and conversion functions
23
+ - **JSON Utilities**: Safe parsing and stringifying operations
22
24
 
23
25
  ## Constants
24
26
 
25
27
  ### Authentication Headers
26
28
  ```typescript
27
- import { apiKeyHeader } from '@onivoro/isomorphic-common';
29
+ import { apiKeyHeader, apiIdHeader, authCookieName } from '@onivoro/isomorphic-common';
28
30
 
29
31
  // Use in HTTP requests
30
32
  const headers = {
31
- [apiKeyHeader]: 'your-api-key' // 'x-api-key'
33
+ [apiKeyHeader]: 'your-api-key', // 'x-api-key'
34
+ [apiIdHeader]: 'api-identifier' // 'x-api-id'
32
35
  };
36
+
37
+ // Cookie name for authentication
38
+ document.cookie = `${authCookieName}=your-token`; // 'token'
33
39
  ```
34
40
 
35
41
  ### Time Constants
@@ -71,6 +77,15 @@ const isValidUuid = v4.test('550e8400-e29b-41d4-a716-446655440000'); // true
71
77
 
72
78
  // Validate ISO date (YYYY-MM-DD)
73
79
  const isValidDate = dateIso8601.test('2023-12-31'); // true
80
+
81
+ // Validate numeric values
82
+ const isNumeric = numeric.test('12345'); // true
83
+
84
+ // Validate SSN (format: XXX-XX-XXXX)
85
+ const isValidSSN = ssn.test('123-45-6789'); // true
86
+
87
+ // Validate EIN (format: XX-XXXXXXX)
88
+ const isValidEIN = ein.test('12-3456789'); // true
74
89
  ```
75
90
 
76
91
  ## String Functions
@@ -93,6 +108,38 @@ snakeCase('Hello World'); // 'hello_world'
93
108
  snakeCase('fooBar'); // 'foo_bar'
94
109
  ```
95
110
 
111
+ ### String Utilities
112
+ ```typescript
113
+ import {
114
+ toString,
115
+ getTag,
116
+ sanitizeFilename,
117
+ fromBooleanString,
118
+ toBooleanString
119
+ } from '@onivoro/isomorphic-common';
120
+
121
+ // toString(value: any): string
122
+ toString(null); // 'null'
123
+ toString([1, 2, 3]); // '1,2,3'
124
+ toString({foo: 'bar'}); // '[object Object]'
125
+
126
+ // getTag(value: any): string
127
+ getTag([]); // '[object Array]'
128
+ getTag(new Date()); // '[object Date]'
129
+
130
+ // sanitizeFilename(filename: string): string
131
+ sanitizeFilename('file:name?.txt'); // 'filename.txt'
132
+ sanitizeFilename('invalid/file\\name'); // 'invalidfilename'
133
+
134
+ // fromBooleanString(value: string): boolean
135
+ fromBooleanString('true'); // true
136
+ fromBooleanString('false'); // false
137
+
138
+ // toBooleanString(value: boolean): string
139
+ toBooleanString(true); // 'true'
140
+ toBooleanString(false); // 'false'
141
+ ```
142
+
96
143
  ## Array Functions
97
144
 
98
145
  ### Array Manipulation
@@ -100,7 +147,10 @@ snakeCase('fooBar'); // 'foo_bar'
100
147
  import {
101
148
  chunk,
102
149
  removeElementAtIndex,
103
- toUniqueArray
150
+ toUniqueArray,
151
+ mapEnumToOptions,
152
+ mapEnumToLookupArray,
153
+ mapEnumToArrayOfValues
104
154
  } from '@onivoro/isomorphic-common';
105
155
 
106
156
  // chunk<T>(array: T[], numDivisions: number): T[][]
@@ -120,7 +170,9 @@ toUniqueArray([1, 2, 2, 3, 3, 4]); // [1, 2, 3, 4]
120
170
  import {
121
171
  sortByName,
122
172
  sortById,
123
- sortNumbers
173
+ sortNumbers,
174
+ sortByPropertyFactory,
175
+ sortByNumericPropertyFactory
124
176
  } from '@onivoro/isomorphic-common';
125
177
 
126
178
  // sortByName<TEntity extends { name: string }>(a: TEntity, b: TEntity): number
@@ -134,6 +186,14 @@ items.sort(sortById); // [{ id: '1' }, { id: '2' }, { id: '3' }]
134
186
  // sortNumbers(a: number, b: number): number
135
187
  const numbers = [3, 1, 4, 1, 5];
136
188
  numbers.sort(sortNumbers); // [1, 1, 3, 4, 5]
189
+
190
+ // sortByPropertyFactory<TEntity>(property: keyof TEntity): (a: TEntity, b: TEntity) => number
191
+ const sortByAge = sortByPropertyFactory<{age: number}>('age');
192
+ const people = [{age: 30}, {age: 20}, {age: 25}];
193
+ people.sort(sortByAge); // [{age: 20}, {age: 25}, {age: 30}]
194
+
195
+ // sortByNumericPropertyFactory<TEntity>(property: keyof TEntity): (a: TEntity, b: TEntity) => number
196
+ const sortByScore = sortByNumericPropertyFactory<{score: number}>('score');
137
197
  ```
138
198
 
139
199
  ## Date/Time Functions
@@ -143,7 +203,10 @@ numbers.sort(sortNumbers); // [1, 1, 3, 4, 5]
143
203
  import {
144
204
  addOffset,
145
205
  subtractOffset,
146
- getDateRangeForMonth
206
+ getDateRangeForMonth,
207
+ getDateLastMonth,
208
+ tryParseDate,
209
+ useDate
147
210
  } from '@onivoro/isomorphic-common';
148
211
 
149
212
  // addOffset(input: string | Date | undefined | null): Date | undefined
@@ -155,6 +218,16 @@ const withoutOffset = subtractOffset(date); // Subtracts timezone offset
155
218
 
156
219
  // getDateRangeForMonth(year: number, month: number): {startDate: Date, endDate: Date}
157
220
  const { startDate, endDate } = getDateRangeForMonth(2023, 0); // January 2023 (month 0-indexed)
221
+
222
+ // getDateLastMonth(): string
223
+ const lastMonth = getDateLastMonth(); // Returns date string for last month
224
+
225
+ // tryParseDate(value: any): Date | null
226
+ const parsedDate = tryParseDate('2023-01-15'); // Date object
227
+ const failedParse = tryParseDate('invalid'); // null
228
+
229
+ // useDate(dateString: string | Date): Date
230
+ const ensuredDate = useDate('2023-01-15'); // Always returns Date object
158
231
  ```
159
232
 
160
233
  ### Date Utilities
@@ -203,12 +276,36 @@ round(19.999, 100); // 20.00 (rounds to nearest cent)
203
276
  round(19.994, 100); // 19.99
204
277
  ```
205
278
 
279
+ ## Object Utilities
280
+
281
+ ```typescript
282
+ import {
283
+ propertiesToArray,
284
+ convertObjectToLiteral,
285
+ toDecimalBase
286
+ } from '@onivoro/isomorphic-common';
287
+
288
+ // propertiesToArray(obj: any, parentKey?: string): string[]
289
+ const obj = { a: { b: { c: 1 } }, d: 2 };
290
+ propertiesToArray(obj); // ['a.b.c', 'd']
291
+
292
+ // convertObjectToLiteral(obj: any): string
293
+ const literal = convertObjectToLiteral({ foo: 'bar', num: 42 });
294
+ // Returns string representation of object
295
+
296
+ // toDecimalBase(num: string | number, base: number): number
297
+ toDecimalBase('FF', 16); // 255
298
+ toDecimalBase('101', 2); // 5
299
+ ```
300
+
206
301
  ## Data Transformation
207
302
 
208
303
  ### Enum Utilities
209
304
  ```typescript
210
305
  import {
211
- mapEnumToOptions
306
+ mapEnumToOptions,
307
+ mapEnumToLookupArray,
308
+ mapEnumToArrayOfValues
212
309
  } from '@onivoro/isomorphic-common';
213
310
 
214
311
  enum Status {
@@ -223,6 +320,14 @@ const options = mapEnumToOptions(Status);
223
320
 
224
321
  const optionsNoBlank = mapEnumToOptions(Status, false);
225
322
  // [{ display: 'ACTIVE', value: 'active' }, ...]
323
+
324
+ // mapEnumToLookupArray<TEntity extends object>(enumeration: TEntity)
325
+ const lookupArray = mapEnumToLookupArray(Status);
326
+ // Array of lookup objects with display/value pairs
327
+
328
+ // mapEnumToArrayOfValues<TEntity extends object>(enumeration: TEntity)
329
+ const values = mapEnumToArrayOfValues(Status);
330
+ // ['active', 'inactive', 'pending']
226
331
  ```
227
332
 
228
333
  ### Entity Utilities
@@ -252,6 +357,28 @@ const json = tryJsonStringify({name: 'John'}); // '{"name":"John"}'
252
357
  const formatted = tryJsonStringify({name: 'John'}, null, 2); // Pretty formatted JSON
253
358
  ```
254
359
 
360
+ ## Testing Utilities
361
+
362
+ ```typescript
363
+ import {
364
+ arrangeActAssert,
365
+ mockCalls
366
+ } from '@onivoro/isomorphic-common';
367
+
368
+ // arrangeActAssert<T>(arrange: () => T, act: (arranged: T) => any, assert: (arranged: T, acted: any) => any)
369
+ arrangeActAssert(
370
+ () => ({ value: 5 }), // arrange
371
+ (arranged) => arranged.value * 2, // act
372
+ (arranged, result) => expect(result).toBe(10) // assert
373
+ );
374
+
375
+ // mockCalls(mockFn: jest.Mock): any[]
376
+ const mock = jest.fn();
377
+ mock('first');
378
+ mock('second');
379
+ const calls = mockCalls(mock); // ['first', 'second']
380
+ ```
381
+
255
382
  ## Utility Functions
256
383
 
257
384
  ```typescript
@@ -264,11 +391,16 @@ await sleep(); // Wait 0 milliseconds (next tick)
264
391
 
265
392
  ## Type Definitions
266
393
 
267
- ### Interfaces
394
+ ### Core Interfaces
268
395
  ```typescript
269
396
  import {
270
397
  ILookup,
271
- TNameable
398
+ TNameable,
399
+ IAccessToken,
400
+ TCreateable,
401
+ TKeysOf,
402
+ IEntityProvider,
403
+ IAxiosWrappedNestException
272
404
  } from '@onivoro/isomorphic-common';
273
405
 
274
406
  // Lookup interface for key-value pairs
@@ -282,6 +414,30 @@ const user: TNameable = {
282
414
  firstName: 'John',
283
415
  lastName: 'Doe'
284
416
  };
417
+
418
+ // Access token interface
419
+ const token: IAccessToken = {
420
+ token: 'jwt-token-here',
421
+ expires: 1234567890
422
+ };
423
+
424
+ // Createable type with timestamp
425
+ const entity: TCreateable = {
426
+ createdAt: new Date()
427
+ };
428
+
429
+ // Type utility for extracting keys with specific value types
430
+ type StringKeys = TKeysOf<{a: string, b: number, c: string}, string>; // 'a' | 'c'
431
+
432
+ // Entity provider interface
433
+ const provider: IEntityProvider = {
434
+ // Implementation details depend on usage
435
+ };
436
+
437
+ // Axios error wrapper for NestJS
438
+ const error: IAxiosWrappedNestException = {
439
+ // Error structure for HTTP exception handling
440
+ };
285
441
  ```
286
442
 
287
443
  ## License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onivoro/isomorphic-common",
3
- "version": "24.30.12",
3
+ "version": "24.30.13",
4
4
  "license": "MIT",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
package/src/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export * from './lib/functions/map-entities-to-options.function';
14
14
  export * from './lib/functions/map-enum-to-array-of-values.function';
15
15
  export * from './lib/functions/map-enum-to-lookup-array.function';
16
16
  export * from './lib/functions/map-enum-to-options.function';
17
+ export * from './lib/functions/profile-time.function';
17
18
  export * from './lib/functions/remove-element-at-index.function';
18
19
  export * from './lib/functions/round.function';
19
20
  export * from './lib/functions/sort-by-number-factory.function';
package/src/index.js CHANGED
@@ -17,6 +17,7 @@ tslib_1.__exportStar(require("./lib/functions/map-entities-to-options.function")
17
17
  tslib_1.__exportStar(require("./lib/functions/map-enum-to-array-of-values.function"), exports);
18
18
  tslib_1.__exportStar(require("./lib/functions/map-enum-to-lookup-array.function"), exports);
19
19
  tslib_1.__exportStar(require("./lib/functions/map-enum-to-options.function"), exports);
20
+ tslib_1.__exportStar(require("./lib/functions/profile-time.function"), exports);
20
21
  tslib_1.__exportStar(require("./lib/functions/remove-element-at-index.function"), exports);
21
22
  tslib_1.__exportStar(require("./lib/functions/round.function"), exports);
22
23
  tslib_1.__exportStar(require("./lib/functions/sort-by-number-factory.function"), exports);
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/isomorphic/common/src/index.ts"],"names":[],"mappings":";;;AAAA,iFAAuD;AACvD,kFAAwD;AACxD,oFAA0D;AAC1D,2EAAiD;AAEjD,sFAA4D;AAC5D,8EAAoD;AAEpD,yEAA+C;AAC/C,8EAAoD;AACpD,uFAA6D;AAC7D,4FAAkE;AAClE,sFAA4D;AAC5D,iFAAuD;AACvD,2FAAiE;AACjE,+FAAqE;AACrE,4FAAkE;AAClE,uFAA6D;AAC7D,2FAAiE;AACjE,yEAA+C;AAC/C,0FAAgE;AAChE,0FAAgE;AAChE,sFAA4D;AAC5D,qFAA2D;AAC3D,8EAAoD;AACpD,gFAAsD;AACtD,gFAAsD;AACtD,8FAAoE;AACpE,4EAAkD;AAElD,6EAAmD;AACnD,mEAAyC;AACzC,sEAA4C;AAC5C,mEAAyC;AACzC,uEAA6C;AAC7C,oEAA0C;AAE1C,kFAAwD;AACxD,mFAAyD;AACzD,qFAA2D;AAC3D,2EAAiD;AAEjD,8EAAoD;AACpD,8EAAoD;AACpD,6FAAmE;AACnE,8EAAoD;AACpD,uFAA6D;AAC7D,sFAA4D;AAC5D,mFAAyD;AACzD,4FAAkE;AAClE,6EAAmD;AACnD,iFAAuD;AACvD,8EAAoD;AACpD,+FAAqE;AACrE,4FAAkE;AAClE,uFAA6D;AAC7D,yEAA+C;AAC/C,8EAAoD;AACpD,uFAA6D;AAC7D,sFAA4D;AAC5D,2FAAiE;AACjE,yEAA+C;AAC/C,yEAA+C;AAC/C,qFAA2D;AAC3D,8EAAoD;AACpD,mFAAyD;AACzD,qFAA2D;AAC3D,oFAA0D;AAC1D,iFAAuD;AACvD,mFAAyD;AACzD,wFAA8D;AAC9D,mFAAyD;AACzD,4EAAkD;AAClD,kFAAwD;AACxD,8EAAoD;AACpD,+EAAqD;AACrD,yEAA+C;AAE/C,6FAAmE;AACnE,gFAAsD;AACtD,mEAAyC;AACzC,uEAA6C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/isomorphic/common/src/index.ts"],"names":[],"mappings":";;;AAAA,iFAAuD;AACvD,kFAAwD;AACxD,oFAA0D;AAC1D,2EAAiD;AAEjD,sFAA4D;AAC5D,8EAAoD;AAEpD,yEAA+C;AAC/C,8EAAoD;AACpD,uFAA6D;AAC7D,4FAAkE;AAClE,sFAA4D;AAC5D,iFAAuD;AACvD,2FAAiE;AACjE,+FAAqE;AACrE,4FAAkE;AAClE,uFAA6D;AAC7D,gFAAsD;AACtD,2FAAiE;AACjE,yEAA+C;AAC/C,0FAAgE;AAChE,0FAAgE;AAChE,sFAA4D;AAC5D,qFAA2D;AAC3D,8EAAoD;AACpD,gFAAsD;AACtD,gFAAsD;AACtD,8FAAoE;AACpE,4EAAkD;AAElD,6EAAmD;AACnD,mEAAyC;AACzC,sEAA4C;AAC5C,mEAAyC;AACzC,uEAA6C;AAC7C,oEAA0C;AAE1C,kFAAwD;AACxD,mFAAyD;AACzD,qFAA2D;AAC3D,2EAAiD;AAEjD,8EAAoD;AACpD,8EAAoD;AACpD,6FAAmE;AACnE,8EAAoD;AACpD,uFAA6D;AAC7D,sFAA4D;AAC5D,mFAAyD;AACzD,4FAAkE;AAClE,6EAAmD;AACnD,iFAAuD;AACvD,8EAAoD;AACpD,+FAAqE;AACrE,4FAAkE;AAClE,uFAA6D;AAC7D,yEAA+C;AAC/C,8EAAoD;AACpD,uFAA6D;AAC7D,sFAA4D;AAC5D,2FAAiE;AACjE,yEAA+C;AAC/C,yEAA+C;AAC/C,qFAA2D;AAC3D,8EAAoD;AACpD,mFAAyD;AACzD,qFAA2D;AAC3D,oFAA0D;AAC1D,iFAAuD;AACvD,mFAAyD;AACzD,wFAA8D;AAC9D,mFAAyD;AACzD,4EAAkD;AAClD,kFAAwD;AACxD,8EAAoD;AACpD,+EAAqD;AACrD,yEAA+C;AAE/C,6FAAmE;AACnE,gFAAsD;AACtD,mEAAyC;AACzC,uEAA6C"}
@@ -0,0 +1 @@
1
+ export declare function profileTime<T>(fn: () => Promise<T>): Promise<T>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.profileTime = profileTime;
4
+ const tslib_1 = require("tslib");
5
+ const PROFILE_TIME = 'PROFILE_TIME';
6
+ function profileTime(fn) {
7
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
8
+ const start = Date.now();
9
+ console.log({ PROFILE_TIME, fn: fn.toString(), start });
10
+ const _ = yield fn();
11
+ const end = Date.now();
12
+ console.log({ PROFILE_TIME, fn: fn.toString(), end, elapsed: (end - start) / 1000 });
13
+ return _;
14
+ });
15
+ }
16
+ //# sourceMappingURL=profile-time.function.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"profile-time.function.js","sourceRoot":"","sources":["../../../../../../../libs/isomorphic/common/src/lib/functions/profile-time.function.ts"],"names":[],"mappings":";;AAEA,kCAQC;;AAVD,MAAM,YAAY,GAAG,cAAc,CAAC;AAEpC,SAAsB,WAAW,CAAI,EAAoB;;QACrD,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACzB,OAAO,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACxD,MAAM,CAAC,GAAG,MAAM,EAAE,EAAE,CAAC;QACrB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,OAAO,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC;QAErF,OAAO,CAAC,CAAC;IACb,CAAC;CAAA"}