@trayio/commons 4.49.0 → 4.50.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.
@@ -0,0 +1,12 @@
1
+ import * as E from 'fp-ts/lib/Either';
2
+ import { Codec } from './Codec';
3
+ export declare class SnakeCasePropertyNameCodec implements Codec<unknown> {
4
+ static instance: Codec<unknown>;
5
+ encode(value: unknown): unknown;
6
+ decode(value: unknown): E.Either<Error, unknown>;
7
+ private isObject;
8
+ private snakeCasePropertyNameIfPresent;
9
+ private snakeCasePropertyNameFromArrayIfPresent;
10
+ private snakeCasePropertyNameFromObjectIfPresent;
11
+ }
12
+ //# sourceMappingURL=SnakeCasePropertyNameCodec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SnakeCasePropertyNameCodec.d.ts","sourceRoot":"","sources":["../../src/codec/SnakeCasePropertyNameCodec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,kBAAkB,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAGhC,qBAAa,0BAA2B,YAAW,KAAK,CAAC,OAAO,CAAC;IAChE,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,CAAoC;IAEnE,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;IAI/B,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC;IAIhD,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,8BAA8B;IAatC,OAAO,CAAC,uCAAuC;IAW/C,OAAO,CAAC,wCAAwC;CAehD"}
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.SnakeCasePropertyNameCodec = void 0;
27
+ const E = __importStar(require("fp-ts/lib/Either"));
28
+ const StringExtensions_1 = require("../string/StringExtensions");
29
+ class SnakeCasePropertyNameCodec {
30
+ static instance = new SnakeCasePropertyNameCodec();
31
+ encode(value) {
32
+ return this.snakeCasePropertyNameIfPresent(value);
33
+ }
34
+ decode(value) {
35
+ return E.right(this.snakeCasePropertyNameIfPresent(value));
36
+ }
37
+ isObject(value) {
38
+ return typeof value === 'object' && !Array.isArray(value) && value !== null;
39
+ }
40
+ snakeCasePropertyNameIfPresent(value) {
41
+ if (value === null) {
42
+ return value;
43
+ }
44
+ if (this.isObject(value)) {
45
+ return this.snakeCasePropertyNameFromObjectIfPresent(value);
46
+ }
47
+ if (Array.isArray(value)) {
48
+ return this.snakeCasePropertyNameFromArrayIfPresent(value);
49
+ }
50
+ return value;
51
+ }
52
+ snakeCasePropertyNameFromArrayIfPresent(value) {
53
+ return value.reduce((acc, propertyValue) => {
54
+ if (propertyValue == null) {
55
+ return acc;
56
+ }
57
+ return [...acc, this.snakeCasePropertyNameIfPresent(propertyValue)];
58
+ }, []);
59
+ }
60
+ snakeCasePropertyNameFromObjectIfPresent(value) {
61
+ return Object.entries(value).reduce((acc, [key, propertyValue]) => {
62
+ if (this.isObject(propertyValue)) {
63
+ const nestedObject = this.snakeCasePropertyNameFromObjectIfPresent(propertyValue);
64
+ return { ...acc, [key]: nestedObject };
65
+ }
66
+ if (Array.isArray(propertyValue)) {
67
+ const nestedArray = this.snakeCasePropertyNameFromArrayIfPresent(propertyValue);
68
+ return { ...acc, [key]: nestedArray };
69
+ }
70
+ return { ...acc, [StringExtensions_1.StringExtensions.snakeCase(key)]: propertyValue };
71
+ }, {});
72
+ }
73
+ }
74
+ exports.SnakeCasePropertyNameCodec = SnakeCasePropertyNameCodec;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=SnakeCasePropertyNameCodec.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SnakeCasePropertyNameCodec.unit.test.d.ts","sourceRoot":"","sources":["../../src/codec/SnakeCasePropertyNameCodec.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const SnakeCasePropertyNameCodec_1 = require("./SnakeCasePropertyNameCodec");
4
+ const testSnakeCasePropertyNameCodec = SnakeCasePropertyNameCodec_1.SnakeCasePropertyNameCodec.instance;
5
+ describe('SnakeCasePropertyNameCodec', () => {
6
+ test('should snake case all properties names', () => {
7
+ const value = {
8
+ firstName: 'blah',
9
+ LastName: null,
10
+ addresses: [
11
+ {
12
+ street: 'blah street',
13
+ number: 123,
14
+ city: {
15
+ name: 'London',
16
+ province: null,
17
+ state: null,
18
+ },
19
+ notes_id: 'blah blah blah',
20
+ },
21
+ {
22
+ street: 'blah blah street',
23
+ number: 321,
24
+ city: {
25
+ name: 'New York',
26
+ province: null,
27
+ state: 'NY',
28
+ },
29
+ notesId: null,
30
+ },
31
+ ],
32
+ phone: {
33
+ Area_Code: null,
34
+ TelephoneNumber: 1234567,
35
+ },
36
+ };
37
+ const actualValue = testSnakeCasePropertyNameCodec.encode(value);
38
+ const expectedValue = {
39
+ first_name: 'blah',
40
+ last_name: null,
41
+ addresses: [
42
+ {
43
+ street: 'blah street',
44
+ number: 123,
45
+ city: {
46
+ name: 'London',
47
+ province: null,
48
+ state: null,
49
+ },
50
+ notes_id: 'blah blah blah',
51
+ },
52
+ {
53
+ street: 'blah blah street',
54
+ number: 321,
55
+ city: {
56
+ name: 'New York',
57
+ province: null,
58
+ state: 'NY',
59
+ },
60
+ notes_id: null,
61
+ },
62
+ ],
63
+ phone: {
64
+ area_code: null,
65
+ telephone_number: 1234567,
66
+ },
67
+ };
68
+ expect(actualValue).toEqual(expectedValue);
69
+ });
70
+ });
@@ -2,6 +2,7 @@ export interface StringExtensionsInterface {
2
2
  pascalCase: (str: string) => string;
3
3
  titleCase: (str: string) => string;
4
4
  camelCase: (str: string) => string;
5
+ snakeCase: (str: string) => string;
5
6
  }
6
7
  export declare const StringExtensions: StringExtensionsInterface;
7
8
  //# sourceMappingURL=StringExtensions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"StringExtensions.d.ts","sourceRoot":"","sources":["../../src/string/StringExtensions.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,yBAAyB;IACzC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;IACpC,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;IACnC,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CACnC;AAED,eAAO,MAAM,gBAAgB,EAAE,yBAK9B,CAAC"}
1
+ {"version":3,"file":"StringExtensions.d.ts","sourceRoot":"","sources":["../../src/string/StringExtensions.ts"],"names":[],"mappings":"AAQA,MAAM,WAAW,yBAAyB;IACzC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;IACpC,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;IACnC,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;IACnC,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CACnC;AAED,eAAO,MAAM,gBAAgB,EAAE,yBAM9B,CAAC"}
@@ -6,4 +6,5 @@ exports.StringExtensions = {
6
6
  pascalCase: (str) => (0, lodash_1.upperFirst)((0, lodash_1.camelCase)(str)),
7
7
  titleCase: (value) => (0, lodash_1.capitalize)((0, lodash_1.startCase)(value).replace(/_|-/g, ' ')),
8
8
  camelCase: (str) => (0, lodash_1.camelCase)(str),
9
+ snakeCase: (str) => (0, lodash_1.snakeCase)(str),
9
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.49.0",
3
+ "version": "4.50.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"