@node-cli/utilities 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.
- package/LICENSE +21 -0
- package/README.md +102 -0
- package/dist/index.d.ts +5 -0
- package/dist/utilities.d.ts +50 -0
- package/dist/utilities.js +58 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Arno Versini
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Node CLI Utilities
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
> A few common utilities for Node CLI applications
|
|
6
|
+
|
|
7
|
+
## Table of Content
|
|
8
|
+
|
|
9
|
+
- [Table of Content](#table-of-content)
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [API](#api)
|
|
12
|
+
- [lowerFirst](#lowerfirst)
|
|
13
|
+
- [fastMerge](#fastmerge)
|
|
14
|
+
- [uniqueID](#uniqueid)
|
|
15
|
+
- [upperFirst](#upperfirst)
|
|
16
|
+
- [License](#license)
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
> cd your-project
|
|
22
|
+
> npm install --save-dev @node-cli/utilities
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## API
|
|
26
|
+
|
|
27
|
+
### lowerFirst
|
|
28
|
+
|
|
29
|
+
**lowerFirst(text: string) ⇒ `string`**
|
|
30
|
+
|
|
31
|
+
Transform the first letter of the provided string to lowercase (but not all the words).
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { lowerFirst } from "@node-cli/utilities";
|
|
35
|
+
const str = lowerFirst("HELLO WORLD");
|
|
36
|
+
// str is "hELLO WORLD"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### fastMerge
|
|
40
|
+
|
|
41
|
+
**fastMerge(objA: object, objB: object, customizer: () => void) ⇒ `object`**
|
|
42
|
+
|
|
43
|
+
Wrapper method for lodash `merge()` and `mergeWith()` methods.
|
|
44
|
+
|
|
45
|
+
Without the `customizer` function, this method recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.
|
|
46
|
+
|
|
47
|
+
With the `customizer` function, the behavior is the same except that `customizer` is invoked to produce the merged values of the destination and source properties. If customizer returns undefined, merging is handled by the `fastMerge` instead. The customizer is invoked with six arguments: `(objValue, srcValue, key, object, source, stack)`
|
|
48
|
+
|
|
49
|
+
**WARNING**: this method will mutate objA!
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { fastMerge } from "@node-cli/utilities";
|
|
53
|
+
|
|
54
|
+
const objA = { port: 123, cache: false, gzip: true };
|
|
55
|
+
const objB = { port: 456, gzip: false };
|
|
56
|
+
const objC = fastMerge(objA, objB);
|
|
57
|
+
|
|
58
|
+
// objC is { port: 456, cache: false, gzip: false };
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import { isArray } from "lodash-es";
|
|
63
|
+
import { fastMerge } from "@node-cli/utilities";
|
|
64
|
+
|
|
65
|
+
const objA = { a: [1], b: [2] };
|
|
66
|
+
const objB = { a: [3], b: [4] };
|
|
67
|
+
const objC = fastMerge(objA, objB, (objValue, srcValue) => {
|
|
68
|
+
if (isArray(objValue)) {
|
|
69
|
+
return objValue.concat(srcValue);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// objC is { 'a': [1, 3], 'b': [2, 4] };
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### uniqueID
|
|
77
|
+
|
|
78
|
+
**uniqueID(prefix?: string = "") ⇒ `string`**
|
|
79
|
+
|
|
80
|
+
Generate a unique ID.
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import { uniqueID } from "@node-cli/utilities";
|
|
84
|
+
const id = uniqueID("my-prefix-");
|
|
85
|
+
// id is something like "my-prefix-5f3a2b3c-1a2b-3c4d-5e6f-7a8b9c0d1e2f"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### upperFirst
|
|
89
|
+
|
|
90
|
+
**upperFirst(text: string) ⇒ `string`**
|
|
91
|
+
|
|
92
|
+
Capitalize the first letter of the provided string (but not all the words).
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
import { upperFirst } from "@node-cli/utilities";
|
|
96
|
+
const str = upperFirst("hello world");
|
|
97
|
+
// str is "Hello world"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT © Arno Versini
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts the first character of string to upper case
|
|
3
|
+
* @param {string} string_ the string to convert
|
|
4
|
+
* @returns {string} the converted string
|
|
5
|
+
*/
|
|
6
|
+
export declare const upperFirst: (string_: string) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Converts the first character of string to lower case
|
|
9
|
+
* @param {string} string_ the string to convert
|
|
10
|
+
* @returns {string} the converted string
|
|
11
|
+
*/
|
|
12
|
+
export declare const lowerFirst: (string_: string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Generate a random number to append to an `id` string.
|
|
15
|
+
*
|
|
16
|
+
* NOTE: we are still using the good old lodash uniqueId when
|
|
17
|
+
* the code is not in production, so that the results are a
|
|
18
|
+
* little bit more consistent tests after test instead of
|
|
19
|
+
* being completely random, so that they do not break
|
|
20
|
+
* potential snapshot tests.
|
|
21
|
+
*
|
|
22
|
+
* @param {String} prefix - When a prefix is provided, the
|
|
23
|
+
* function will return a random
|
|
24
|
+
* number appended to the provided
|
|
25
|
+
* prefix.
|
|
26
|
+
*
|
|
27
|
+
* @returns {String} - Returns a string with random numbers.
|
|
28
|
+
*/
|
|
29
|
+
export declare const uniqueID: (prefix?: string) => string;
|
|
30
|
+
/**
|
|
31
|
+
* Wrapper method for lodash `merge()` and `mergeWith()` methods.
|
|
32
|
+
*
|
|
33
|
+
* Without the `customizer` function, this method recursively merges own and inherited
|
|
34
|
+
* enumerable string keyed properties of source objects into the destination object.
|
|
35
|
+
* Source properties that resolve to undefined are skipped if a destination value exists.
|
|
36
|
+
* Array and plain object properties are merged recursively. Other objects and value
|
|
37
|
+
* types are overridden by assignment. Source objects are applied from left to right.
|
|
38
|
+
* Subsequent sources overwrite property assignments of previous sources.
|
|
39
|
+
*
|
|
40
|
+
* With the `customizer` function, the behavior is the same except that `customizer` is
|
|
41
|
+
* invoked to produce the merged values of the destination and source properties.
|
|
42
|
+
* If customizer returns undefined, merging is handled by the `fastMerge` instead.
|
|
43
|
+
* The customizer is invoked with six arguments: `(objValue, srcValue, key, object,
|
|
44
|
+
* source, stack)`
|
|
45
|
+
* @param {object} objectA
|
|
46
|
+
* @param {object} objectB
|
|
47
|
+
* @param {function} customizer
|
|
48
|
+
* @returns {object} !! WARNING: this method will mutate objectA
|
|
49
|
+
*/
|
|
50
|
+
export declare const fastMerge: (objectA: any, objectB: any, customizer?: any) => object;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { merge, mergeWith, uniqueId } from "lodash-es";
|
|
2
|
+
/**
|
|
3
|
+
* Converts the first character of string to upper case
|
|
4
|
+
* @param {string} string_ the string to convert
|
|
5
|
+
* @returns {string} the converted string
|
|
6
|
+
*/ export const upperFirst = (string_)=>string_[0].toUpperCase() + string_.slice(1);
|
|
7
|
+
/**
|
|
8
|
+
* Converts the first character of string to lower case
|
|
9
|
+
* @param {string} string_ the string to convert
|
|
10
|
+
* @returns {string} the converted string
|
|
11
|
+
*/ export const lowerFirst = (string_)=>string_[0].toLowerCase() + string_.slice(1);
|
|
12
|
+
/**
|
|
13
|
+
* Generate a random number to append to an `id` string.
|
|
14
|
+
*
|
|
15
|
+
* NOTE: we are still using the good old lodash uniqueId when
|
|
16
|
+
* the code is not in production, so that the results are a
|
|
17
|
+
* little bit more consistent tests after test instead of
|
|
18
|
+
* being completely random, so that they do not break
|
|
19
|
+
* potential snapshot tests.
|
|
20
|
+
*
|
|
21
|
+
* @param {String} prefix - When a prefix is provided, the
|
|
22
|
+
* function will return a random
|
|
23
|
+
* number appended to the provided
|
|
24
|
+
* prefix.
|
|
25
|
+
*
|
|
26
|
+
* @returns {String} - Returns a string with random numbers.
|
|
27
|
+
*/ export const uniqueID = (prefix = "")=>{
|
|
28
|
+
if (process.env.NODE_ENV !== "production") {
|
|
29
|
+
return uniqueId(prefix);
|
|
30
|
+
}
|
|
31
|
+
// Extract the decimal part
|
|
32
|
+
const randomNumber = `${Math.random()}`.split(".")[1];
|
|
33
|
+
return prefix || prefix !== "" ? `${prefix}${randomNumber}` : randomNumber;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Wrapper method for lodash `merge()` and `mergeWith()` methods.
|
|
37
|
+
*
|
|
38
|
+
* Without the `customizer` function, this method recursively merges own and inherited
|
|
39
|
+
* enumerable string keyed properties of source objects into the destination object.
|
|
40
|
+
* Source properties that resolve to undefined are skipped if a destination value exists.
|
|
41
|
+
* Array and plain object properties are merged recursively. Other objects and value
|
|
42
|
+
* types are overridden by assignment. Source objects are applied from left to right.
|
|
43
|
+
* Subsequent sources overwrite property assignments of previous sources.
|
|
44
|
+
*
|
|
45
|
+
* With the `customizer` function, the behavior is the same except that `customizer` is
|
|
46
|
+
* invoked to produce the merged values of the destination and source properties.
|
|
47
|
+
* If customizer returns undefined, merging is handled by the `fastMerge` instead.
|
|
48
|
+
* The customizer is invoked with six arguments: `(objValue, srcValue, key, object,
|
|
49
|
+
* source, stack)`
|
|
50
|
+
* @param {object} objectA
|
|
51
|
+
* @param {object} objectB
|
|
52
|
+
* @param {function} customizer
|
|
53
|
+
* @returns {object} !! WARNING: this method will mutate objectA
|
|
54
|
+
*/ export const fastMerge = (objectA, objectB, customizer)=>{
|
|
55
|
+
return typeof customizer === "function" ? mergeWith(objectA, objectB, customizer) : merge(objectA, objectB);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=utilities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utilities.ts"],"sourcesContent":["import { merge, mergeWith, uniqueId } from \"lodash-es\";\n\n/**\n * Converts the first character of string to upper case\n * @param {string} string_ the string to convert\n * @returns {string} the converted string\n */\nexport const upperFirst = (string_: string): string =>\n\tstring_[0].toUpperCase() + string_.slice(1);\n\n/**\n * Converts the first character of string to lower case\n * @param {string} string_ the string to convert\n * @returns {string} the converted string\n */\nexport const lowerFirst = (string_: string): string =>\n\tstring_[0].toLowerCase() + string_.slice(1);\n\n/**\n * Generate a random number to append to an `id` string.\n *\n * NOTE: we are still using the good old lodash uniqueId when\n * the code is not in production, so that the results are a\n * little bit more consistent tests after test instead of\n * being completely random, so that they do not break\n * potential snapshot tests.\n *\n * @param {String} prefix - When a prefix is provided, the\n * function will return a random\n * number appended to the provided\n * prefix.\n *\n * @returns {String} - Returns a string with random numbers.\n */\nexport const uniqueID = (prefix: string = \"\"): string => {\n\tif (process.env.NODE_ENV !== \"production\") {\n\t\treturn uniqueId(prefix);\n\t}\n\t// Extract the decimal part\n\tconst randomNumber = `${Math.random()}`.split(\".\")[1];\n\treturn prefix || prefix !== \"\" ? `${prefix}${randomNumber}` : randomNumber;\n};\n\n/**\n * Wrapper method for lodash `merge()` and `mergeWith()` methods.\n *\n * Without the `customizer` function, this method recursively merges own and inherited\n * enumerable string keyed properties of source objects into the destination object.\n * Source properties that resolve to undefined are skipped if a destination value exists.\n * Array and plain object properties are merged recursively. Other objects and value\n * types are overridden by assignment. Source objects are applied from left to right.\n * Subsequent sources overwrite property assignments of previous sources.\n *\n * With the `customizer` function, the behavior is the same except that `customizer` is\n * invoked to produce the merged values of the destination and source properties.\n * If customizer returns undefined, merging is handled by the `fastMerge` instead.\n * The customizer is invoked with six arguments: `(objValue, srcValue, key, object,\n * source, stack)`\n * @param {object} objectA\n * @param {object} objectB\n * @param {function} customizer\n * @returns {object} !! WARNING: this method will mutate objectA\n */\nexport const fastMerge = (\n\tobjectA: any,\n\tobjectB: any,\n\tcustomizer?: any\n): object => {\n\treturn typeof customizer === \"function\"\n\t\t? mergeWith(objectA, objectB, customizer)\n\t\t: merge(objectA, objectB);\n};\n"],"names":["merge","mergeWith","uniqueId","upperFirst","string_","toUpperCase","slice","lowerFirst","toLowerCase","uniqueID","prefix","process","env","NODE_ENV","randomNumber","Math","random","split","fastMerge","objectA","objectB","customizer"],"mappings":"AAAA,SAASA,KAAK,EAAEC,SAAS,EAAEC,QAAQ,QAAQ,YAAY;AAEvD;;;;CAIC,GACD,OAAO,MAAMC,aAAa,CAACC,UAC1BA,OAAO,CAAC,EAAE,CAACC,gBAAgBD,QAAQE,MAAM,GAAG;AAE7C;;;;CAIC,GACD,OAAO,MAAMC,aAAa,CAACH,UAC1BA,OAAO,CAAC,EAAE,CAACI,gBAAgBJ,QAAQE,MAAM,GAAG;AAE7C;;;;;;;;;;;;;;;CAeC,GACD,OAAO,MAAMG,WAAW,CAACC,SAAiB,EAAE;IAC3C,IAAIC,QAAQC,IAAIC,aAAa,cAAc;QAC1C,OAAOX,SAASQ;IACjB;IACA,2BAA2B;IAC3B,MAAMI,eAAe,CAAC,EAAEC,KAAKC,SAAS,CAAC,CAACC,MAAM,IAAI,CAAC,EAAE;IACrD,OAAOP,UAAUA,WAAW,KAAK,CAAC,EAAEA,OAAO,EAAEI,aAAa,CAAC,GAAGA;AAC/D,EAAE;AAEF;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,MAAMI,YAAY,CACxBC,SACAC,SACAC;IAEA,OAAO,OAAOA,eAAe,aAC1BpB,UAAUkB,SAASC,SAASC,cAC5BrB,MAAMmB,SAASC;AACnB,EAAE"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@node-cli/utilities",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Arno Versini",
|
|
6
|
+
"description": "A few utilities for Node CLI applications",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": "./dist/utilities.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"node": ">=16",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"lodash-es": "4.17.21"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "yarn run clean && yarn run build:types && yarn run build:js && yarn run build:barrel",
|
|
19
|
+
"build:barrel": "barrelsby --delete --directory dist --pattern \"**/*.d.ts\" --name \"index.d\"",
|
|
20
|
+
"build:js": "swc --source-maps --out-dir dist src",
|
|
21
|
+
"build:types": "tsc",
|
|
22
|
+
"clean": "rimraf dist types coverage",
|
|
23
|
+
"lint": "prettier --write \"src/*.ts\" && eslint --fix \"src/*.ts\"",
|
|
24
|
+
"test": "cross-env-shell NODE_OPTIONS=--experimental-vm-modules jest",
|
|
25
|
+
"test:coverage": "npm run test -- --coverage",
|
|
26
|
+
"watch": "swc --watch --out-dir dist src"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "bbdae6c2e303b431aa3e49aa9a3c86365449f06f"
|
|
32
|
+
}
|