@avarin/primitives 0.0.0 → 0.0.1

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,64 @@
1
+ export type Enum = NodeJS.Dict<string | number>;
2
+ export declare namespace Enum {
3
+ /**
4
+ * Returns the keys of the Enum.
5
+ *
6
+ * @param enumObj Enum object to be inspected.
7
+ * @returns Collection of the keys of the Enum.
8
+ */
9
+ function getKeys<T extends Enum>(enumObj: T): (keyof T)[];
10
+ /**
11
+ * Returns the key of the first Enum member that represents the provided value.
12
+ *
13
+ * @param enumObj Enum object to be inspected.
14
+ * @param value Value to be searched.
15
+ * @returns Key of the first Enum member based on the provided value.
16
+ */
17
+ function getKey<T extends Enum>(enumObj: T, value: T[keyof T]): keyof T | null;
18
+ /**
19
+ * Checks if the provided Enum has the provided key as its member.
20
+ *
21
+ * @param enumObj Enum object to be inspected.
22
+ * @param key Key to be checked.
23
+ * @returns Whether the provided Enum has the provided key as its member.
24
+ */
25
+ function hasKey<T extends Enum>(enumObj: Readonly<T>, key: unknown): boolean;
26
+ /**
27
+ * Returns the values of the Enum.
28
+ *
29
+ * @param enumObj Enum object to be inspected.
30
+ * @returns Collection of the Values of the Enum.
31
+ */
32
+ function getValues<T extends Enum>(enumObj: T): T[keyof T][];
33
+ /**
34
+ * Returns the first occurrence of the value in the Enum based on the provided key.
35
+ *
36
+ * @param enumObj Enum object to be inspected.
37
+ * @param key Key to be searched.
38
+ * @returns First occurrence of the value in the Enum.
39
+ */
40
+ function getValue<T extends Enum>(enumObj: T, key: keyof T): T[keyof T] | null;
41
+ /**
42
+ * Checks if the provided Enum has the provided value as its member.
43
+ *
44
+ * @param enumObj Enum object to be inspected.
45
+ * @param value Value to be checked.
46
+ * @returns Whether the provided Enum has the provided value as its member.
47
+ */
48
+ function hasValue<T extends Enum>(enumObj: Readonly<T>, value: unknown): boolean;
49
+ /**
50
+ * Converts the provided value into a member of the provided Enum.
51
+ *
52
+ * @param enumObj Enum object to be inspected.
53
+ * @param value Value to be searched.
54
+ * @returns Enum member based on the provided value.
55
+ */
56
+ function parse<T extends Enum>(enumObj: T, value: string | number): T[keyof T] | null;
57
+ /**
58
+ * Returns the entries of the provided Enum.
59
+ *
60
+ * @param enumObj Enum object to be inspected.
61
+ * @returns Entries of the provided Enum.
62
+ */
63
+ function getEntries<T extends Enum>(enumObj: T): [keyof T, T[keyof T]][];
64
+ }
@@ -0,0 +1,87 @@
1
+ // oxlint-disable-next-line no-redeclare
2
+ (function(Enum) {
3
+ function getKeys(enumObj) {
4
+ return Object.keys(enumObj).filter((key)=>Number.isNaN(Number(key)));
5
+ }
6
+ /**
7
+ * Returns the keys of the Enum.
8
+ *
9
+ * @param enumObj Enum object to be inspected.
10
+ * @returns Collection of the keys of the Enum.
11
+ */ Enum.getKeys = getKeys;
12
+ function getKey(enumObj, value) {
13
+ var _getKeys_find;
14
+ return (_getKeys_find = getKeys(enumObj).find((key)=>enumObj[key] === value)) != null ? _getKeys_find : null;
15
+ }
16
+ /**
17
+ * Returns the key of the first Enum member that represents the provided value.
18
+ *
19
+ * @param enumObj Enum object to be inspected.
20
+ * @param value Value to be searched.
21
+ * @returns Key of the first Enum member based on the provided value.
22
+ */ Enum.getKey = getKey;
23
+ function hasKey(enumObj, key) {
24
+ return typeof key === "string" && getKeys(enumObj).includes(key);
25
+ }
26
+ /**
27
+ * Checks if the provided Enum has the provided key as its member.
28
+ *
29
+ * @param enumObj Enum object to be inspected.
30
+ * @param key Key to be checked.
31
+ * @returns Whether the provided Enum has the provided key as its member.
32
+ */ Enum.hasKey = hasKey;
33
+ function getValues(enumObj) {
34
+ return getKeys(enumObj).map((key)=>enumObj[key]);
35
+ }
36
+ /**
37
+ * Returns the values of the Enum.
38
+ *
39
+ * @param enumObj Enum object to be inspected.
40
+ * @returns Collection of the Values of the Enum.
41
+ */ Enum.getValues = getValues;
42
+ function getValue(enumObj, key) {
43
+ return hasKey(enumObj, key) ? enumObj[key] : null;
44
+ }
45
+ /**
46
+ * Returns the first occurrence of the value in the Enum based on the provided key.
47
+ *
48
+ * @param enumObj Enum object to be inspected.
49
+ * @param key Key to be searched.
50
+ * @returns First occurrence of the value in the Enum.
51
+ */ Enum.getValue = getValue;
52
+ function hasValue(enumObj, value) {
53
+ // @ts-expect-error Value is runtime safe.
54
+ return (typeof value === "string" || typeof value === "number") && getValues(enumObj).includes(value);
55
+ }
56
+ /**
57
+ * Checks if the provided Enum has the provided value as its member.
58
+ *
59
+ * @param enumObj Enum object to be inspected.
60
+ * @param value Value to be checked.
61
+ * @returns Whether the provided Enum has the provided value as its member.
62
+ */ Enum.hasValue = hasValue;
63
+ function parse(enumObj, value) {
64
+ var _getValues_find;
65
+ return (_getValues_find = getValues(enumObj).find((attr)=>attr === value)) != null ? _getValues_find : null;
66
+ }
67
+ /**
68
+ * Converts the provided value into a member of the provided Enum.
69
+ *
70
+ * @param enumObj Enum object to be inspected.
71
+ * @param value Value to be searched.
72
+ * @returns Enum member based on the provided value.
73
+ */ Enum.parse = parse;
74
+ function getEntries(enumObj) {
75
+ return getKeys(enumObj).map((key)=>[
76
+ key,
77
+ enumObj[key]
78
+ ]);
79
+ }
80
+ /**
81
+ * Returns the entries of the provided Enum.
82
+ *
83
+ * @param enumObj Enum object to be inspected.
84
+ * @returns Entries of the provided Enum.
85
+ */ Enum.getEntries = getEntries;
86
+ })(Enum || (Enum = {}));
87
+ export var Enum;
@@ -0,0 +1,25 @@
1
+ export type JSON = string | number | boolean | NodeJS.Dict<JSON> | {
2
+ toJSON: (...args: readonly unknown[]) => JSON;
3
+ } | JSON[] | null;
4
+ export declare namespace JSON {
5
+ /**
6
+ * Converts a JavaScript Object Notation (JSON) string into an object.
7
+ *
8
+ * @param text A valid JSON string.
9
+ * @param reviver A function that transforms the results. This function is called for each member of the object. If a
10
+ * member contains nested objects, the nested objects are transformed before the parent object is.
11
+ * @returns Parsed object from the JSON string.
12
+ */
13
+ function parse<T extends JSON>(text: string, reviver?: (this: unknown, key: string, value: T[keyof T]) => T[keyof T]): T;
14
+ /**
15
+ * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
16
+ *
17
+ * @param value A JavaScript value, usually an object or array, to be converted.
18
+ * @param replacer A function that transforms the results or an array of strings and numbers that acts as an approved
19
+ * list for selecting the object properties that will be stringified.
20
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it
21
+ * easier to read.
22
+ * @returns Stringified object.
23
+ */
24
+ function stringify<T extends JSON>(value: T, replacer?: (this: unknown, key: string, value: T[keyof T]) => T | ((number | string)[] | null), space?: string | number): string;
25
+ }
@@ -0,0 +1,42 @@
1
+ // oxlint-disable-next-line no-redeclare
2
+ (function(JSON) {
3
+ /**
4
+ * JSON Reviver function that prevents prototype and constructor pollution.
5
+ *
6
+ * @param key JSON object key.
7
+ * @param value JSON object value.
8
+ * @param reviver Reviver function that transforms the results.
9
+ * @returns Parsed object without forbidden properties.
10
+ */ function stripForbiddenProperties(key, value, reviver) {
11
+ if (key === "__proto__" || key === "constructor") {
12
+ return;
13
+ }
14
+ return typeof reviver === "function" ? reviver(key, value) : value;
15
+ }
16
+ function parse(text, reviver) {
17
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
18
+ return globalThis.JSON.parse(text, (key, value)=>stripForbiddenProperties(key, value, reviver));
19
+ }
20
+ /**
21
+ * Converts a JavaScript Object Notation (JSON) string into an object.
22
+ *
23
+ * @param text A valid JSON string.
24
+ * @param reviver A function that transforms the results. This function is called for each member of the object. If a
25
+ * member contains nested objects, the nested objects are transformed before the parent object is.
26
+ * @returns Parsed object from the JSON string.
27
+ */ JSON.parse = parse;
28
+ function stringify(value, replacer, space) {
29
+ return globalThis.JSON.stringify(value, replacer, space);
30
+ }
31
+ /**
32
+ * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
33
+ *
34
+ * @param value A JavaScript value, usually an object or array, to be converted.
35
+ * @param replacer A function that transforms the results or an array of strings and numbers that acts as an approved
36
+ * list for selecting the object properties that will be stringified.
37
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it
38
+ * easier to read.
39
+ * @returns Stringified object.
40
+ */ JSON.stringify = stringify;
41
+ })(JSON || (JSON = {}));
42
+ export var JSON;
@@ -0,0 +1,17 @@
1
+ export declare namespace Objects {
2
+ /**
3
+ * Checks if the provided data is a Plain Javascript Object.
4
+ *
5
+ * @param data Object to be checked.
6
+ * @returns The provided data is a Plain Javascript Object.
7
+ */
8
+ function isPlain(data: unknown): data is object;
9
+ /**
10
+ * Removes null and undefined values from the properties of an object or an array of objects. This method mutates the
11
+ * provided object in-place.
12
+ *
13
+ * @param data Object or array of objects to be cleansed.
14
+ * @returns Provided object without null and undefined properties.
15
+ */
16
+ function removeNullishValues<T = unknown>(data: T): T;
17
+ }
@@ -0,0 +1,48 @@
1
+ // oxlint-disable-next-line typescript/no-namespace
2
+ (function(Objects) {
3
+ function isPlain(data) {
4
+ if (typeof data !== "object" || data === null) {
5
+ return false;
6
+ }
7
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
8
+ const proto = Object.getPrototypeOf(data);
9
+ return proto === null || proto === Object.prototype || Object.getPrototypeOf(proto) === null;
10
+ }
11
+ /**
12
+ * Checks if the provided data is a Plain Javascript Object.
13
+ *
14
+ * @param data Object to be checked.
15
+ * @returns The provided data is a Plain Javascript Object.
16
+ */ Objects.isPlain = isPlain;
17
+ function removeNullishValues(data) {
18
+ if (typeof data !== "object" || data === null) {
19
+ return data;
20
+ }
21
+ for (let [key, value] of Object.entries(data)){
22
+ if (value == null) {
23
+ Reflect.deleteProperty(data, key);
24
+ continue;
25
+ }
26
+ if (Array.isArray(value)) {
27
+ // oxlint-disable-next-line typescript/no-unsafe-return
28
+ value = value.filter((v)=>v != null).map((v)=>removeNullishValues(v));
29
+ Reflect.set(data, key, value);
30
+ continue;
31
+ }
32
+ if (typeof value === "object") {
33
+ removeNullishValues(value);
34
+ Reflect.set(data, key, value);
35
+ continue;
36
+ }
37
+ }
38
+ return data;
39
+ }
40
+ /**
41
+ * Removes null and undefined values from the properties of an object or an array of objects. This method mutates the
42
+ * provided object in-place.
43
+ *
44
+ * @param data Object or array of objects to be cleansed.
45
+ * @returns Provided object without null and undefined properties.
46
+ */ Objects.removeNullishValues = removeNullishValues;
47
+ })(Objects || (Objects = {}));
48
+ export var Objects;
@@ -0,0 +1,9 @@
1
+ export declare namespace Strings {
2
+ /**
3
+ * Checks if the provided string is empty.
4
+ *
5
+ * @param str String to be checked.
6
+ * @returns Whether or not the provided string is empty.
7
+ */
8
+ function isEmpty(str: string): boolean;
9
+ }
@@ -0,0 +1,13 @@
1
+ // oxlint-disable-next-line typescript/no-namespace
2
+ (function(Strings) {
3
+ function isEmpty(str) {
4
+ return str.trim() === "";
5
+ }
6
+ /**
7
+ * Checks if the provided string is empty.
8
+ *
9
+ * @param str String to be checked.
10
+ * @returns Whether or not the provided string is empty.
11
+ */ Strings.isEmpty = isEmpty;
12
+ })(Strings || (Strings = {}));
13
+ export var Strings;
@@ -0,0 +1,4 @@
1
+ /** Denotes the Abstract Constructor Type of the provided Generic. */
2
+ export type AbstractConstructor<T> = abstract new (...args: readonly unknown[]) => T;
3
+ /** Denotes the Constructor Type of the provided Generic. */
4
+ export type Constructor<T> = new (...args: readonly unknown[]) => T;
@@ -0,0 +1 @@
1
+ /** Denotes the Abstract Constructor Type of the provided Generic. */ /** Denotes the Constructor Type of the provided Generic. */ export { };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avarin/primitives",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "Enhanced primitives functionalities for NodeJS.",
5
5
  "homepage": "https://github.com/avarin-org/node/tree/main/packages/primitives#readme",
6
6
  "bugs": {
@@ -22,7 +22,37 @@
22
22
  ],
23
23
  "type": "module",
24
24
  "exports": {
25
- "./package.json": "./package.json"
25
+ "./package.json": "./package.json",
26
+ "./enum": {
27
+ "@avarin/source": "./src/lib/enum/enum.ts",
28
+ "types": "./dist/lib/enum/enum.d.ts",
29
+ "import": "./dist/lib/enum/enum.js",
30
+ "default": "./dist/lib/enum/enum.js"
31
+ },
32
+ "./json": {
33
+ "@avarin/source": "./src/lib/json/json.ts",
34
+ "types": "./dist/lib/json/json.d.ts",
35
+ "import": "./dist/lib/json/json.js",
36
+ "default": "./dist/lib/json/json.js"
37
+ },
38
+ "./objects": {
39
+ "@avarin/source": "./src/lib/objects/objects.ts",
40
+ "types": "./dist/lib/objects/objects.d.ts",
41
+ "import": "./dist/lib/objects/objects.js",
42
+ "default": "./dist/lib/objects/objects.js"
43
+ },
44
+ "./strings": {
45
+ "@avarin/source": "./src/lib/strings/strings.ts",
46
+ "types": "./dist/lib/strings/strings.d.ts",
47
+ "import": "./dist/lib/strings/strings.js",
48
+ "default": "./dist/lib/strings/strings.js"
49
+ },
50
+ "./types": {
51
+ "@avarin/source": "./src/lib/types/types.ts",
52
+ "types": "./dist/lib/types/types.d.ts",
53
+ "import": "./dist/lib/types/types.js",
54
+ "default": "./dist/lib/types/types.js"
55
+ }
26
56
  },
27
57
  "dependencies": {
28
58
  "@swc/helpers": "~0.5.18"