@poppinss/utils 6.3.1-0 → 6.5.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/README.md +27 -4
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/src/import_default.d.ts +3 -0
- package/build/src/import_default.js +15 -0
- package/build/src/string_builder.d.ts +23 -0
- package/build/src/string_builder.js +86 -0
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -556,6 +556,17 @@ string.bytes.format(1024 * 1024 * 1000) // 1000MB
|
|
|
556
556
|
string.bytes.format(1024 * 1024 * 1000, { thousandsSeparator: ',' }) // 1,000MB
|
|
557
557
|
```
|
|
558
558
|
|
|
559
|
+
### String builder
|
|
560
|
+
The string builder offers a fluent API for applying a set of transforms on a string value. You can create an instance of the string builder as follows.
|
|
561
|
+
|
|
562
|
+
```ts
|
|
563
|
+
import StringBuilder from '@poppinss/utils/string_builder'
|
|
564
|
+
const builder = new StringBuilder('hello world')
|
|
565
|
+
|
|
566
|
+
const value = builder.snakeCase().suffix('_controller').toString()
|
|
567
|
+
assert(value === 'hello_world_controller')
|
|
568
|
+
```
|
|
569
|
+
|
|
559
570
|
### JSON helpers
|
|
560
571
|
|
|
561
572
|
Following are the helpers we use to `stringify` and `parse` JSON.
|
|
@@ -772,6 +783,7 @@ throw new ResourceNotFound()
|
|
|
772
783
|
```
|
|
773
784
|
|
|
774
785
|
#### Anonymous error classes
|
|
786
|
+
|
|
775
787
|
You can also create an anonymous exception class using the `createError` method. The return value is a class
|
|
776
788
|
constructor that accepts an array of values to use for interpolation.
|
|
777
789
|
|
|
@@ -779,7 +791,10 @@ The interpolation of error message is performed using the `util.format` message.
|
|
|
779
791
|
|
|
780
792
|
```ts
|
|
781
793
|
import { createError } from '@poppinss/utils'
|
|
782
|
-
const E_RESOURCE_NOT_FOUND = createError(
|
|
794
|
+
const E_RESOURCE_NOT_FOUND = createError(
|
|
795
|
+
'Unable to find resource with id %d',
|
|
796
|
+
'E_RESOURCE_NOT_FOUND'
|
|
797
|
+
)
|
|
783
798
|
|
|
784
799
|
const id = 1
|
|
785
800
|
throw new E_RESOURCE_NOT_FOUND([id])
|
|
@@ -922,6 +937,16 @@ await Promise.all(
|
|
|
922
937
|
)
|
|
923
938
|
```
|
|
924
939
|
|
|
940
|
+
#### importDefault
|
|
941
|
+
A helper function that assert a lazy import function output to have a `default export`, otherwise raises an exception.
|
|
942
|
+
|
|
943
|
+
We use dynamic default exports a lot in AdonisJS apps, so extracting the check to a helper function.
|
|
944
|
+
|
|
945
|
+
```ts
|
|
946
|
+
import { importDefault } from '@poppinss/utils'
|
|
947
|
+
const defaultVal = await importDefault(() => import('./some_module.js'))
|
|
948
|
+
```
|
|
949
|
+
|
|
925
950
|
#### naturalSort
|
|
926
951
|
|
|
927
952
|
A sorting function to use natural sort for ordering an array.
|
|
@@ -1039,9 +1064,7 @@ Instead of writing conditionals, you can consider using the Object builder fluen
|
|
|
1039
1064
|
```ts
|
|
1040
1065
|
const builder = new ObjectBuilder({ a: 1 })
|
|
1041
1066
|
|
|
1042
|
-
const plainObject = builder
|
|
1043
|
-
.add('b', b)
|
|
1044
|
-
.toObject()
|
|
1067
|
+
const plainObject = builder.add('b', b).toObject()
|
|
1045
1068
|
```
|
|
1046
1069
|
|
|
1047
1070
|
By default, only the `undefined` values are ignored. However, you can also ignore `null` values.
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
export { base64 } from './src/base64.js';
|
|
3
3
|
export { compose } from './src/compose.js';
|
|
4
|
+
export { importDefault } from './src/import_default.js';
|
|
4
5
|
export { defineStaticProperty } from './src/define_static_property.js';
|
|
5
6
|
export { Exception, createError } from './src/exception.js';
|
|
6
7
|
export { flatten } from './src/flatten.js';
|
package/build/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
2
2
|
import { dirname as pathDirname } from 'node:path';
|
|
3
3
|
export { base64 } from './src/base64.js';
|
|
4
4
|
export { compose } from './src/compose.js';
|
|
5
|
+
export { importDefault } from './src/import_default.js';
|
|
5
6
|
export { defineStaticProperty } from './src/define_static_property.js';
|
|
6
7
|
export { Exception, createError } from './src/exception.js';
|
|
7
8
|
export { flatten } from './src/flatten.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RuntimeException } from './exceptions/runtime_exception.js';
|
|
2
|
+
export async function importDefault(importFn, filePath) {
|
|
3
|
+
const moduleExports = await importFn();
|
|
4
|
+
if (!('default' in moduleExports)) {
|
|
5
|
+
const errorMessage = filePath
|
|
6
|
+
? `Missing "export default" in module "${filePath}"`
|
|
7
|
+
: `Missing "export default" from lazy import "${importFn}"`;
|
|
8
|
+
throw new RuntimeException(errorMessage, {
|
|
9
|
+
cause: {
|
|
10
|
+
source: importFn,
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return moduleExports.default;
|
|
15
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default class StringBuilder {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(value: string | StringBuilder);
|
|
4
|
+
dashCase(): this;
|
|
5
|
+
dotCase(): this;
|
|
6
|
+
snakeCase(): this;
|
|
7
|
+
pascalCase(): this;
|
|
8
|
+
camelCase(): this;
|
|
9
|
+
capitalCase(): this;
|
|
10
|
+
titleCase(): this;
|
|
11
|
+
sentenceCase(): this;
|
|
12
|
+
noCase(): this;
|
|
13
|
+
plural(): this;
|
|
14
|
+
singular(): this;
|
|
15
|
+
slugify(): this;
|
|
16
|
+
removeSuffix(suffix: string): this;
|
|
17
|
+
suffix(suffix: string): this;
|
|
18
|
+
removePrefix(prefix: string): this;
|
|
19
|
+
prefix(prefix: string): this;
|
|
20
|
+
removeExtension(): this;
|
|
21
|
+
ext(extension: string): this;
|
|
22
|
+
toString(): string;
|
|
23
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { extname } from 'node:path';
|
|
2
|
+
import string from './string/main.js';
|
|
3
|
+
export default class StringBuilder {
|
|
4
|
+
#value;
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.#value = typeof value === 'string' ? value : value.toString();
|
|
7
|
+
}
|
|
8
|
+
dashCase() {
|
|
9
|
+
this.#value = string.dashCase(this.#value);
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
dotCase() {
|
|
13
|
+
this.#value = string.dotCase(this.#value);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
snakeCase() {
|
|
17
|
+
this.#value = string.snakeCase(this.#value);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
pascalCase() {
|
|
21
|
+
this.#value = string.pascalCase(this.#value);
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
camelCase() {
|
|
25
|
+
this.#value = string.camelCase(this.#value);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
capitalCase() {
|
|
29
|
+
this.#value = string.capitalCase(this.#value);
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
titleCase() {
|
|
33
|
+
this.#value = string.titleCase(this.#value);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
sentenceCase() {
|
|
37
|
+
this.#value = string.sentenceCase(this.#value);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
noCase() {
|
|
41
|
+
this.#value = string.noCase(this.#value);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
plural() {
|
|
45
|
+
this.#value = string.pluralize(this.#value);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
singular() {
|
|
49
|
+
this.#value = string.singular(this.#value);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
slugify() {
|
|
53
|
+
this.#value = string.slug(this.#value);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
removeSuffix(suffix) {
|
|
57
|
+
this.#value = this.#value.replace(new RegExp(`[-_]?${suffix}$`, 'i'), '');
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
suffix(suffix) {
|
|
61
|
+
this.removeSuffix(suffix);
|
|
62
|
+
this.#value = `${this.#value}${suffix}`;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
removePrefix(prefix) {
|
|
66
|
+
this.#value = this.#value.replace(new RegExp(`^${prefix}[-_]?`, 'i'), '');
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
prefix(prefix) {
|
|
70
|
+
this.removePrefix(prefix);
|
|
71
|
+
this.#value = `${prefix}${this.#value}`;
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
removeExtension() {
|
|
75
|
+
this.#value = this.#value.replace(new RegExp(`${extname(this.#value)}$`), '');
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
ext(extension) {
|
|
79
|
+
this.removeExtension();
|
|
80
|
+
this.#value = `${this.#value}.${extension.replace(/^\./, '')}`;
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
toString() {
|
|
84
|
+
return this.#value;
|
|
85
|
+
}
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@poppinss/utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.5.0-0",
|
|
4
4
|
"description": "Handy utilities for repetitive work",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"node": "./build/lodash/main.cjs"
|
|
19
19
|
},
|
|
20
20
|
"./string": "./build/src/string/main.js",
|
|
21
|
+
"./string_builder": "./build/src/string_builder.js",
|
|
21
22
|
"./json": "./build/src/json/main.js",
|
|
22
23
|
"./types": "./build/src/types.js"
|
|
23
24
|
},
|
|
@@ -53,30 +54,30 @@
|
|
|
53
54
|
"author": "virk,poppinss",
|
|
54
55
|
"license": "MIT",
|
|
55
56
|
"devDependencies": {
|
|
56
|
-
"@commitlint/cli": "^17.
|
|
57
|
-
"@commitlint/config-conventional": "^17.
|
|
57
|
+
"@commitlint/cli": "^17.4.2",
|
|
58
|
+
"@commitlint/config-conventional": "^17.4.2",
|
|
58
59
|
"@japa/assert": "^1.3.6",
|
|
59
60
|
"@japa/expect-type": "^1.0.2",
|
|
60
61
|
"@japa/run-failed-tests": "^1.1.0",
|
|
61
62
|
"@japa/runner": "^2.2.2",
|
|
62
63
|
"@japa/spec-reporter": "^1.3.2",
|
|
63
|
-
"@swc/core": "^1.3.
|
|
64
|
-
"@types/fs-extra": "^
|
|
64
|
+
"@swc/core": "^1.3.29",
|
|
65
|
+
"@types/fs-extra": "^11.0.1",
|
|
65
66
|
"@types/node": "^18.11.18",
|
|
66
67
|
"c8": "^7.12.0",
|
|
67
68
|
"del-cli": "^5.0.0",
|
|
68
|
-
"eslint": "^8.
|
|
69
|
-
"eslint-config-prettier": "^8.
|
|
69
|
+
"eslint": "^8.32.0",
|
|
70
|
+
"eslint-config-prettier": "^8.6.0",
|
|
70
71
|
"eslint-plugin-adonis": "^3.0.3",
|
|
71
72
|
"eslint-plugin-prettier": "^4.2.1",
|
|
72
73
|
"fs-extra": "^11.1.0",
|
|
73
74
|
"github-label-sync": "^2.2.0",
|
|
74
|
-
"husky": "^8.0.
|
|
75
|
+
"husky": "^8.0.3",
|
|
75
76
|
"lodash": "^4.17.21",
|
|
76
77
|
"lodash-cli": "^4.17.5",
|
|
77
78
|
"move-file-cli": "^3.0.0",
|
|
78
79
|
"np": "^7.6.3",
|
|
79
|
-
"prettier": "^2.8.
|
|
80
|
+
"prettier": "^2.8.3",
|
|
80
81
|
"ts-node": "^10.9.1",
|
|
81
82
|
"typescript": "^4.9.4"
|
|
82
83
|
},
|
|
@@ -89,7 +90,7 @@
|
|
|
89
90
|
"flattie": "^1.1.0",
|
|
90
91
|
"pluralize": "^8.0.0",
|
|
91
92
|
"safe-stable-stringify": "^2.4.2",
|
|
92
|
-
"secure-json-parse": "^2.
|
|
93
|
+
"secure-json-parse": "^2.7.0",
|
|
93
94
|
"slash": "^5.0.0",
|
|
94
95
|
"slugify": "^1.6.5",
|
|
95
96
|
"truncatise": "^0.0.8"
|