@poppinss/utils 3.2.1 → 4.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.
package/README.md CHANGED
@@ -113,6 +113,7 @@ string.titleCase()
113
113
  - [Safe stringify](#safe-stringify)
114
114
  - [Safe parse](#safe-parse)
115
115
  - [defineStaticProperty](#definestaticproperty)
116
+ - [flatten](#flatten)
116
117
  - [Helpers](#helpers)
117
118
  - [fsReadAll](#fsreadall)
118
119
  - [requireAll](#requireall)
@@ -379,6 +380,48 @@ The `defineStaticProperty` takes a total of three arguments.
379
380
  - The `inherit` strategy will copy the properties from the base class.
380
381
  - The `define` strategy will always use the `defaultValue` to define the property on the class. In other words, there is no copy behavior, but prototypal inheritance chain is also breaked by explicitly re-defining the property.
381
382
 
383
+ ## flatten
384
+ Flatten an object/array. The method wraps the [flattie](https://github.com/lukeed/flattie) package.
385
+
386
+ ```ts
387
+ import { flatten } from '@poppinss/utils'
388
+
389
+ flatten({
390
+ a: 'hi',
391
+ b: {
392
+ a: null,
393
+ b: ['foo', '', null, 'bar'],
394
+ d: 'hello',
395
+ e: {
396
+ a: 'yo',
397
+ b: undefined,
398
+ c: 'sup',
399
+ d: 0,
400
+ f: [
401
+ { foo: 123, bar: 123 },
402
+ { foo: 465, bar: 456 },
403
+ ]
404
+ }
405
+ },
406
+ c: 'world'
407
+ });
408
+ // {
409
+ // 'a': 'hi',
410
+ // 'b.b.0': 'foo',
411
+ // 'b.b.1': '',
412
+ // 'b.b.3': 'bar',
413
+ // 'b.d': 'hello',
414
+ // 'b.e.a': 'yo',
415
+ // 'b.e.c': 'sup',
416
+ // 'b.e.d': 0,
417
+ // 'b.e.f.0.foo': 123,
418
+ // 'b.e.f.0.bar': 123,
419
+ // 'b.e.f.1.foo': 465,
420
+ // 'b.e.f.1.bar': 456,
421
+ // 'c': 'world'
422
+ // }
423
+ ```
424
+
382
425
  ## Helpers
383
426
 
384
427
  The helpers module is also available in AdonisJS applications as follows:
@@ -426,6 +469,16 @@ const config = requireAll(join(__dirname, 'config'))
426
469
  }
427
470
  ```
428
471
 
472
+ The method also accepts the following options
473
+
474
+ ```ts
475
+ requireAll(join(__dirname, 'config'), recursive, optional, filter)
476
+ ```
477
+
478
+ - `recursive` Load all files recursively. Defaults to true.
479
+ - `optional` Do not raise exception when the root directory is missing. Defaults to false.
480
+ - `filter` Cherry pick files to require. By default, all JavaScript, TypeScript and JSON files are required.
481
+
429
482
  ### resolveFrom
430
483
 
431
484
  Works similar to `require.resolve`, however it handles the absolute paths properly.
package/build/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * as lodash from './src/lodash';
2
2
  export { slash } from './src/slash';
3
+ export { flatten } from './src/flatten';
3
4
  export { Exception } from './src/Exception';
4
5
  export { safeParse } from './src/safeParse';
5
6
  export { esmRequire } from './src/esmRequire';
package/build/index.js CHANGED
@@ -27,10 +27,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
27
27
  return result;
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ManagerConfigValidator = exports.defineStaticProperty = exports.safeStringify = exports.esmResolver = exports.esmRequire = exports.safeParse = exports.Exception = exports.slash = exports.lodash = void 0;
30
+ exports.ManagerConfigValidator = exports.defineStaticProperty = exports.safeStringify = exports.esmResolver = exports.esmRequire = exports.safeParse = exports.Exception = exports.flatten = exports.slash = exports.lodash = void 0;
31
31
  exports.lodash = __importStar(require("./src/lodash"));
32
32
  var slash_1 = require("./src/slash");
33
33
  Object.defineProperty(exports, "slash", { enumerable: true, get: function () { return slash_1.slash; } });
34
+ var flatten_1 = require("./src/flatten");
35
+ Object.defineProperty(exports, "flatten", { enumerable: true, get: function () { return flatten_1.flatten; } });
34
36
  var Exception_1 = require("./src/Exception");
35
37
  Object.defineProperty(exports, "Exception", { enumerable: true, get: function () { return Exception_1.Exception; } });
36
38
  var safeParse_1 = require("./src/safeParse");
@@ -3,6 +3,6 @@
3
3
  * for files ending with `.ts`, `.js` and `.json`. Also files ending with
4
4
  * `.d.ts` are ignored.
5
5
  */
6
- export declare function requireAll(location: string, recursive?: boolean, optional?: boolean): {
6
+ export declare function requireAll(location: string, recursive?: boolean, optional?: boolean, filter?: (file: string) => boolean | string): {
7
7
  [key: string]: any;
8
8
  } | undefined;
@@ -17,26 +17,39 @@ const require_all_1 = __importDefault(require("require-all"));
17
17
  const esmResolver_1 = require("../esmResolver");
18
18
  const isScriptFile_1 = require("../isScriptFile");
19
19
  /**
20
- * Function to filter selected files only
20
+ * Function to filter and keep script files only
21
21
  */
22
22
  function fileFilter(file) {
23
- const ext = (0, path_1.extname)(file);
24
- if (!(0, isScriptFile_1.isScriptFile)(file)) {
25
- return false;
26
- }
27
- return file.replace(new RegExp(`${ext}$`), '');
23
+ return (0, isScriptFile_1.isScriptFile)(file);
28
24
  }
29
25
  /**
30
26
  * Require all files from a given directory. The method automatically looks
31
27
  * for files ending with `.ts`, `.js` and `.json`. Also files ending with
32
28
  * `.d.ts` are ignored.
33
29
  */
34
- function requireAll(location, recursive = true, optional = false) {
30
+ function requireAll(location, recursive = true, optional = false, filter = fileFilter) {
35
31
  try {
36
32
  return (0, require_all_1.default)({
37
33
  dirname: location,
38
34
  recursive,
39
- filter: fileFilter,
35
+ filter: (file) => {
36
+ let result = true;
37
+ /**
38
+ * Invoke user defined function
39
+ */
40
+ if (typeof filter === 'function') {
41
+ result = filter(file);
42
+ }
43
+ /**
44
+ * Use the default file name when file is meant to
45
+ * be kept
46
+ */
47
+ if (result === true) {
48
+ const ext = (0, path_1.extname)(file);
49
+ return file.replace(new RegExp(`${ext}$`), '');
50
+ }
51
+ return result;
52
+ },
40
53
  resolve: esmResolver_1.esmResolver,
41
54
  });
42
55
  }
@@ -7,12 +7,9 @@
7
7
  * For the full copyright and license information, please view the LICENSE
8
8
  * file that was distributed with this source code.
9
9
  */
10
- var __importDefault = (this && this.__importDefault) || function (mod) {
11
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
- };
13
10
  Object.defineProperty(exports, "__esModule", { value: true });
14
11
  exports.safeEqual = void 0;
15
- const buffer_alloc_1 = __importDefault(require("buffer-alloc"));
12
+ const buffer_1 = require("buffer");
16
13
  const crypto_1 = require("crypto");
17
14
  /**
18
15
  * Generates a random string for a given size
@@ -23,23 +20,23 @@ function safeEqual(value, comparisonValue) {
23
20
  * The length of the main value to ensure that we compare equal strings
24
21
  * against each other to get a constant time.
25
22
  */
26
- const expectedLength = Buffer.byteLength(value);
23
+ const expectedLength = buffer_1.Buffer.byteLength(value);
27
24
  /**
28
25
  * Value A
29
26
  */
30
- const valueBuffer = (0, buffer_alloc_1.default)(expectedLength, 0, 'utf-8');
27
+ const valueBuffer = buffer_1.Buffer.alloc(expectedLength, 0, 'utf-8');
31
28
  valueBuffer.write(value);
32
29
  /**
33
30
  * Value B
34
31
  */
35
- const comparisonValueBuffer = (0, buffer_alloc_1.default)(expectedLength, 0, 'utf-8');
32
+ const comparisonValueBuffer = buffer_1.Buffer.alloc(expectedLength, 0, 'utf-8');
36
33
  comparisonValueBuffer.write(comparisonValue);
37
34
  /**
38
35
  * Ensure values are same and also have same length
39
36
  */
40
37
  return ((0, crypto_1.timingSafeEqual)(valueBuffer, comparisonValueBuffer) &&
41
- expectedLength === Buffer.byteLength(comparisonValue));
38
+ expectedLength === buffer_1.Buffer.byteLength(comparisonValue));
42
39
  }
43
- return (0, crypto_1.timingSafeEqual)(Buffer.from(value), Buffer.from(comparisonValue));
40
+ return (0, crypto_1.timingSafeEqual)(buffer_1.Buffer.from(value), buffer_1.Buffer.from(comparisonValue));
44
41
  }
45
42
  exports.safeEqual = safeEqual;
@@ -1,8 +1,9 @@
1
1
  declare type Constructor = new (...args: any[]) => any;
2
+ declare type AbstractConstructor = abstract new (...args: any[]) => any;
2
3
  /**
3
4
  * Define static properties on a class with inheritance in play.
4
5
  */
5
- export declare function defineStaticProperty<T extends Constructor, Prop extends keyof T>(self: T, BaseClass: Constructor, { propertyName, defaultValue, strategy, }: {
6
+ export declare function defineStaticProperty<T extends Constructor | AbstractConstructor, Prop extends keyof T>(self: T, BaseClass: Constructor | AbstractConstructor, { propertyName, defaultValue, strategy, }: {
6
7
  propertyName: Prop;
7
8
  defaultValue: T[Prop];
8
9
  strategy: 'inherit' | 'define' | ((value: T[Prop]) => T[Prop]);
@@ -0,0 +1 @@
1
+ export { flattie as flatten } from 'flattie';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ /*
3
+ * @poppinss/utils
4
+ *
5
+ * (c) Harminder Virk <virk@adonisjs.com>
6
+ *
7
+ * For the full copyright and license information, please view the LICENSE
8
+ * file that was distributed with this source code.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.flatten = void 0;
12
+ var flattie_1 = require("flattie");
13
+ Object.defineProperty(exports, "flatten", { enumerable: true, get: function () { return flattie_1.flattie; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@poppinss/utils",
3
- "version": "3.2.1",
3
+ "version": "4.0.1",
4
4
  "description": "Handy utilities for repetitive work",
5
5
  "main": "build/index.js",
6
6
  "files": [
@@ -14,12 +14,12 @@
14
14
  "scripts": {
15
15
  "mrm": "mrm --preset=@adonisjs/mrm-preset",
16
16
  "pretest": "npm run lint",
17
- "test": "node japaFile.js",
17
+ "test": "node .bin/test.js",
18
18
  "clean": "del build",
19
19
  "compile": "npm run lint && npm run clean && tsc",
20
20
  "build": "npm run compile",
21
21
  "commit": "git-cz",
22
- "release": "np",
22
+ "release": "np --message=\"chore(release): %s\"",
23
23
  "version": "npm run build",
24
24
  "format": "prettier --write .",
25
25
  "prepublishOnly": "npm run build",
@@ -32,28 +32,28 @@
32
32
  "author": "virk,poppinss",
33
33
  "license": "MIT",
34
34
  "devDependencies": {
35
- "@adonisjs/mrm-preset": "^4.1.2",
35
+ "@adonisjs/mrm-preset": "^5.0.2",
36
36
  "@adonisjs/require-ts": "^2.0.8",
37
- "@poppinss/dev-utils": "^1.1.5",
37
+ "@poppinss/dev-utils": "^2.0.1",
38
38
  "@types/fs-readdir-recursive": "^1.0.0",
39
- "@types/lodash": "^4.14.175",
39
+ "@types/lodash": "^4.14.178",
40
40
  "@types/ms": "^0.7.31",
41
- "@types/node": "^16.10.2",
41
+ "@types/node": "^17.0.8",
42
42
  "@types/pluralize": "0.0.29",
43
43
  "@types/require-all": "^3.0.3",
44
44
  "del-cli": "^4.0.1",
45
- "doctoc": "^2.0.1",
46
- "eslint": "^7.32.0",
45
+ "doctoc": "^2.1.0",
46
+ "eslint": "^8.7.0",
47
47
  "eslint-config-prettier": "^8.3.0",
48
- "eslint-plugin-adonis": "^1.3.3",
48
+ "eslint-plugin-adonis": "^2.1.0",
49
49
  "eslint-plugin-prettier": "^4.0.0",
50
50
  "github-label-sync": "^2.0.2",
51
- "husky": "^7.0.2",
52
- "japa": "^3.1.1",
53
- "mrm": "^3.0.9",
54
- "np": "^7.5.0",
55
- "prettier": "^2.4.1",
56
- "typescript": "^4.4.3"
51
+ "husky": "^7.0.4",
52
+ "japa": "^4.0.0",
53
+ "mrm": "^3.0.10",
54
+ "np": "^7.6.0",
55
+ "prettier": "^2.5.1",
56
+ "typescript": "^4.5.4"
57
57
  },
58
58
  "nyc": {
59
59
  "exclude": [
@@ -75,10 +75,10 @@
75
75
  "dependencies": {
76
76
  "@types/bytes": "^3.1.1",
77
77
  "@types/he": "^1.1.2",
78
- "buffer-alloc": "^1.2.0",
79
- "bytes": "^3.1.0",
78
+ "bytes": "^3.1.1",
80
79
  "change-case": "^4.1.2",
81
80
  "cuid": "^2.1.8",
81
+ "flattie": "^1.1.0",
82
82
  "fs-readdir-recursive": "^1.1.0",
83
83
  "he": "^1.2.0",
84
84
  "kind-of": "^6.0.3",
@@ -87,7 +87,7 @@
87
87
  "pluralize": "^8.0.0",
88
88
  "require-all": "^3.0.0",
89
89
  "resolve-from": "^5.0.0",
90
- "slugify": "^1.6.0",
90
+ "slugify": "^1.6.5",
91
91
  "truncatise": "0.0.8"
92
92
  },
93
93
  "directories": {
@@ -101,5 +101,50 @@
101
101
  "bugs": {
102
102
  "url": "https://github.com/poppinss/utils/issues"
103
103
  },
104
- "homepage": "https://github.com/poppinss/utils#readme"
104
+ "homepage": "https://github.com/poppinss/utils#readme",
105
+ "eslintConfig": {
106
+ "extends": [
107
+ "plugin:adonis/typescriptPackage",
108
+ "prettier",
109
+ "prettier"
110
+ ],
111
+ "plugins": [
112
+ "prettier",
113
+ "prettier"
114
+ ],
115
+ "rules": {
116
+ "prettier/prettier": [
117
+ "error",
118
+ {
119
+ "endOfLine": "auto"
120
+ }
121
+ ]
122
+ }
123
+ },
124
+ "eslintIgnore": [
125
+ "build"
126
+ ],
127
+ "prettier": {
128
+ "trailingComma": "es5",
129
+ "semi": false,
130
+ "singleQuote": true,
131
+ "useTabs": false,
132
+ "quoteProps": "consistent",
133
+ "bracketSpacing": true,
134
+ "arrowParens": "always",
135
+ "printWidth": 100
136
+ },
137
+ "mrmConfig": {
138
+ "core": false,
139
+ "license": "MIT",
140
+ "services": [
141
+ "github-actions"
142
+ ],
143
+ "minNodeVersion": "16.13.1",
144
+ "probotApps": [
145
+ "stale",
146
+ "lock"
147
+ ],
148
+ "runGhActionsOnWindows": true
149
+ }
105
150
  }