@deenruv/common 1.0.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,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getGraphQlInputName = exports.generateAllCombinations = exports.summate = exports.isClassInstance = exports.isObject = exports.assertNever = exports.notNullOrUndefined = void 0;
4
+ /**
5
+ * Predicate with type guard, used to filter out null or undefined values
6
+ * in a filter operation.
7
+ */
8
+ function notNullOrUndefined(val) {
9
+ return val !== undefined && val !== null;
10
+ }
11
+ exports.notNullOrUndefined = notNullOrUndefined;
12
+ /**
13
+ * Used in exhaustiveness checks to assert a codepath should never be reached.
14
+ */
15
+ function assertNever(value) {
16
+ throw new Error(`Expected never, got ${typeof value} (${JSON.stringify(value)})`);
17
+ }
18
+ exports.assertNever = assertNever;
19
+ /**
20
+ * Simple object check.
21
+ * From https://stackoverflow.com/a/34749873/772859
22
+ */
23
+ function isObject(item) {
24
+ return item && typeof item === "object" && !Array.isArray(item);
25
+ }
26
+ exports.isObject = isObject;
27
+ function isClassInstance(item) {
28
+ // Even if item is an object, it might not have a constructor as in the
29
+ // case when it is a null-prototype object, i.e. created using `Object.create(null)`.
30
+ return (isObject(item) && item.constructor && item.constructor.name !== "Object");
31
+ }
32
+ exports.isClassInstance = isClassInstance;
33
+ /**
34
+ * Adds up all the values of a given numeric property of a list of objects.
35
+ */
36
+ function summate(items, prop) {
37
+ return (items || []).reduce((sum, i) => sum + i[prop], 0);
38
+ }
39
+ exports.summate = summate;
40
+ /**
41
+ * Given an array of option arrays `[['red, 'blue'], ['small', 'large']]`, this method returns a new array
42
+ * containing all the combinations of those options:
43
+ *
44
+ * @example
45
+ * ```
46
+ * generateAllCombinations([['red, 'blue'], ['small', 'large']]);
47
+ * // =>
48
+ * // [
49
+ * // ['red', 'small'],
50
+ * // ['red', 'large'],
51
+ * // ['blue', 'small'],
52
+ * // ['blue', 'large'],
53
+ * // ]
54
+ */
55
+ function generateAllCombinations(optionGroups, combination = [], k = 0, output = []) {
56
+ if (k === 0) {
57
+ optionGroups = optionGroups.filter((g) => 0 < g.length);
58
+ }
59
+ if (k === optionGroups.length) {
60
+ output.push(combination);
61
+ return [];
62
+ }
63
+ else {
64
+ for (let i = 0; i < optionGroups[k].length; i++) {
65
+ generateAllCombinations(optionGroups, combination.concat(optionGroups[k][i]), k + 1, output);
66
+ }
67
+ return output;
68
+ }
69
+ }
70
+ exports.generateAllCombinations = generateAllCombinations;
71
+ /**
72
+ * @description
73
+ * Returns the input field name of a custom field, taking into account that "relation" type custom
74
+ * field inputs are suffixed with "Id" or "Ids".
75
+ */
76
+ function getGraphQlInputName(config) {
77
+ if (config.type === "relation") {
78
+ return config.list === true ? `${config.name}Ids` : `${config.name}Id`;
79
+ }
80
+ else {
81
+ return config.name;
82
+ }
83
+ }
84
+ exports.getGraphQlInputName = getGraphQlInputName;
85
+ //# sourceMappingURL=shared-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-utils.js","sourceRoot":"","sources":["../src/shared-utils.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,SAAgB,kBAAkB,CAAI,GAAyB;IAC7D,OAAO,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,CAAC;AAC3C,CAAC;AAFD,gDAEC;AAED;;GAEG;AACH,SAAgB,WAAW,CAAC,KAAY;IACtC,MAAM,IAAI,KAAK,CACb,uBAAuB,OAAO,KAAK,KAAK,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CACjE,CAAC;AACJ,CAAC;AAJD,kCAIC;AAED;;;GAGG;AACH,SAAgB,QAAQ,CAAC,IAAS;IAChC,OAAO,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AAClE,CAAC;AAFD,4BAEC;AAED,SAAgB,eAAe,CAAC,IAAS;IACvC,uEAA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AAC,MAInC;IACC,IAAI,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;QAC/B,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,IAAI,IAAI,CAAC;IACzE,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CAAC,IAAI,CAAC;IACrB,CAAC;AACH,CAAC;AAVD,kDAUC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * An extremely fast function for deep-cloning an object which only contains simple
3
+ * values, i.e. primitives, arrays and nested simple objects.
4
+ */
5
+ export declare function simpleDeepClone<T extends string | number | any[] | object>(input: T): T;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.simpleDeepClone = void 0;
4
+ const shared_utils_1 = require("./shared-utils");
5
+ /**
6
+ * An extremely fast function for deep-cloning an object which only contains simple
7
+ * values, i.e. primitives, arrays and nested simple objects.
8
+ */
9
+ function simpleDeepClone(input) {
10
+ // if not array or object or is null return self
11
+ if (typeof input !== "object" || input === null) {
12
+ return input;
13
+ }
14
+ let output;
15
+ let i;
16
+ // handle case: array
17
+ if (input instanceof Array) {
18
+ let l;
19
+ output = [];
20
+ for (i = 0, l = input.length; i < l; i++) {
21
+ output[i] = simpleDeepClone(input[i]);
22
+ }
23
+ return output;
24
+ }
25
+ if ((0, shared_utils_1.isClassInstance)(input)) {
26
+ return input;
27
+ }
28
+ // handle case: object
29
+ output = {};
30
+ for (i in input) {
31
+ if (input.hasOwnProperty(i)) {
32
+ output[i] = simpleDeepClone(input[i]);
33
+ }
34
+ }
35
+ return output;
36
+ }
37
+ exports.simpleDeepClone = simpleDeepClone;
38
+ //# sourceMappingURL=simple-deep-clone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simple-deep-clone.js","sourceRoot":"","sources":["../src/simple-deep-clone.ts"],"names":[],"mappings":";;;AAAA,iDAAiD;AAEjD;;;GAGG;AACH,SAAgB,eAAe,CAC7B,KAAQ;IAER,gDAAgD;IAChD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,MAAW,CAAC;IAChB,IAAI,CAAkB,CAAC;IACvB,qBAAqB;IACrB,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,IAAI,CAAC,CAAC;QACN,MAAM,GAAG,EAAW,CAAC;QACrB,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACzC,MAAM,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,IAAI,IAAA,8BAAe,EAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,sBAAsB;IACtB,MAAM,GAAG,EAAE,CAAC;IACZ,KAAK,CAAC,IAAI,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;YAC5B,MAAM,CAAC,CAAC,CAAC,GAAG,eAAe,CAAE,KAAa,CAAC,CAAC,CAAC,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AA7BD,0CA6BC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @description
3
+ * Returns an array with only unique values. Objects are compared by reference,
4
+ * unless the `byKey` argument is supplied, in which case matching properties will
5
+ * be used to check duplicates
6
+ */
7
+ export declare function unique<T>(arr: T[], byKey?: keyof T): T[];
package/lib/unique.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unique = void 0;
4
+ /**
5
+ * @description
6
+ * Returns an array with only unique values. Objects are compared by reference,
7
+ * unless the `byKey` argument is supplied, in which case matching properties will
8
+ * be used to check duplicates
9
+ */
10
+ function unique(arr, byKey) {
11
+ if (byKey == null) {
12
+ return Array.from(new Set(arr));
13
+ }
14
+ else {
15
+ // Based on https://stackoverflow.com/a/58429784/772859
16
+ return [...new Map(arr.map((item) => [item[byKey], item])).values()];
17
+ }
18
+ }
19
+ exports.unique = unique;
20
+ //# sourceMappingURL=unique.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unique.js","sourceRoot":"","sources":["../src/unique.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,MAAM,CAAI,GAAQ,EAAE,KAAe;IACjD,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAClB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAClC,CAAC;SAAM,CAAC;QACN,uDAAuD;QACvD,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IACvE,CAAC;AACH,CAAC;AAPD,wBAOC"}
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@deenruv/common",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "license": "MIT",
6
+ "homepage": "https://deenruv.com/",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "files": [
11
+ "lib/**/*"
12
+ ],
13
+ "devDependencies": {
14
+ "rimraf": "^5.0.5",
15
+ "typescript": "5.3.3"
16
+ },
17
+ "scripts": {
18
+ "watch": "tsc -p ./tsconfig.build.json -w",
19
+ "build": "rimraf lib && tsc -p ./tsconfig.build.json",
20
+ "lint": "eslint .",
21
+ "lint:fix": "eslint --fix .",
22
+ "test": "vitest --run",
23
+ "ci": "npm run build"
24
+ }
25
+ }