xcraft-core-utils 4.3.4 → 4.3.7

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/.eslintrc.js CHANGED
@@ -1,28 +1,28 @@
1
- 'use strict';
2
-
3
- module.exports = {
4
- root: true,
5
- parserOptions: {
6
- ecmaVersion: 7,
7
- sourceType: 'module',
8
- ecmaFeatures: {
9
- jsx: true,
10
- },
11
- },
12
- env: {
13
- browser: true,
14
- mocha: true,
15
- node: true,
16
- es6: true,
17
- },
18
- parser: 'babel-eslint',
19
- plugins: ['react', 'babel'],
20
- extends: ['prettier', 'eslint:recommended', 'plugin:react/recommended'],
21
- rules: {
22
- // Other rules
23
- 'no-console': 'off',
24
- 'valid-jsdoc': ['error', {requireReturn: false}],
25
- 'eqeqeq': 'error',
26
- 'react/display-name': 'off',
27
- },
28
- };
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ root: true,
5
+ parserOptions: {
6
+ ecmaVersion: 7,
7
+ sourceType: 'module',
8
+ ecmaFeatures: {
9
+ jsx: true,
10
+ },
11
+ },
12
+ env: {
13
+ browser: true,
14
+ mocha: true,
15
+ node: true,
16
+ es6: true,
17
+ },
18
+ parser: 'babel-eslint',
19
+ plugins: ['react', 'babel'],
20
+ extends: ['prettier', 'eslint:recommended', 'plugin:react/recommended'],
21
+ rules: {
22
+ // Other rules
23
+ 'no-console': 'off',
24
+ 'valid-jsdoc': ['error', {requireReturn: false}],
25
+ 'eqeqeq': 'error',
26
+ 'react/display-name': 'off',
27
+ },
28
+ };
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
- # xcraft-core-utils
2
-
3
- Xcraft utils
1
+ # xcraft-core-utils
2
+
3
+ Xcraft utils
package/index.js CHANGED
@@ -1,24 +1,24 @@
1
- 'use strict';
2
-
3
- exports.ArrayCollector = require('./lib/arrayCollector.js');
4
- exports.batch = require('./lib/batch.js');
5
- exports.crypto = require('./lib/crypto.js');
6
- exports.EventDebouncer = require('./lib/eventDebouncer.js');
7
- exports.files = require('./lib/files.js');
8
- exports.fileCrypto = require('./lib/file-crypto.js');
9
- exports.js = require('./lib/js.js');
10
- exports.json = require('./lib/json.js');
11
- exports.locks = require('./lib/locks.js');
12
- exports.log = require('./lib/log.js');
13
- exports.modules = require('./lib/modules.js');
14
- exports.os = require('./lib/os.js');
15
- exports.propTypes = require('./lib/prop-types.js');
16
- exports.reflect = require('./lib/reflect.js');
17
- exports.regex = require('./lib/regex.js');
18
- exports.string = require('./lib/string.js');
19
- exports.whereIs = require('./lib/whereIs.js');
20
- exports.yaml = require('./lib/yaml.js');
21
-
22
- exports.RankedCache = require('./lib/ranked-cache.js');
23
- exports.JobQueue = require('./lib/job-queue.js');
24
- exports.CursorPump = require('./lib/cursorPump.js');
1
+ 'use strict';
2
+
3
+ exports.ArrayCollector = require('./lib/arrayCollector.js');
4
+ exports.batch = require('./lib/batch.js');
5
+ exports.crypto = require('./lib/crypto.js');
6
+ exports.EventDebouncer = require('./lib/eventDebouncer.js');
7
+ exports.files = require('./lib/files.js');
8
+ exports.fileCrypto = require('./lib/file-crypto.js');
9
+ exports.js = require('./lib/js.js');
10
+ exports.json = require('./lib/json.js');
11
+ exports.locks = require('./lib/locks.js');
12
+ exports.log = require('./lib/log.js');
13
+ exports.modules = require('./lib/modules.js');
14
+ exports.os = require('./lib/os.js');
15
+ exports.propTypes = require('./lib/prop-types.js');
16
+ exports.reflect = require('./lib/reflect.js');
17
+ exports.regex = require('./lib/regex.js');
18
+ exports.string = require('./lib/string.js');
19
+ exports.whereIs = require('./lib/whereIs.js');
20
+ exports.yaml = require('./lib/yaml.js');
21
+
22
+ exports.RankedCache = require('./lib/ranked-cache.js');
23
+ exports.JobQueue = require('./lib/job-queue.js');
24
+ exports.CursorPump = require('./lib/cursorPump.js');
package/lib/.babelrc CHANGED
@@ -1,8 +1,8 @@
1
- {
2
- "plugins": [
3
- "@babel/proposal-class-properties",
4
- "@babel/proposal-object-rest-spread",
5
- "@babel/proposal-function-bind",
6
- "@babel/transform-modules-commonjs"
7
- ]
8
- }
1
+ {
2
+ "plugins": [
3
+ "@babel/proposal-class-properties",
4
+ "@babel/proposal-object-rest-spread",
5
+ "@babel/proposal-function-bind",
6
+ "@babel/transform-modules-commonjs"
7
+ ]
8
+ }
@@ -1,33 +1,33 @@
1
- 'use strict';
2
-
3
- const {throttle} = require('lodash');
4
- const watt = require('gigawatts');
5
-
6
- class ArrayCollector {
7
- constructor(resp, wait = 20, onCollect, leading = true) {
8
- this.onCollect = watt(onCollect);
9
- this.entries = {};
10
- this.resp = resp;
11
- this.release = throttle(this._release, wait, {leading});
12
- }
13
-
14
- _release() {
15
- const copy = this.entries;
16
- this.entries = {};
17
- this.onCollect(copy, this.resp);
18
- }
19
-
20
- _addByKey(key, data) {
21
- if (!this.entries[key]) {
22
- this.entries[key] = [];
23
- }
24
- this.entries[key] = this.entries[key].concat(data);
25
- }
26
-
27
- grab(key, data) {
28
- this._addByKey(key, data);
29
- this.release();
30
- }
31
- }
32
-
33
- module.exports = ArrayCollector;
1
+ 'use strict';
2
+
3
+ const {throttle} = require('lodash');
4
+ const watt = require('gigawatts');
5
+
6
+ class ArrayCollector {
7
+ constructor(resp, wait = 20, onCollect, leading = true) {
8
+ this.onCollect = watt(onCollect);
9
+ this.entries = {};
10
+ this.resp = resp;
11
+ this.release = throttle(this._release, wait, {leading});
12
+ }
13
+
14
+ _release() {
15
+ const copy = this.entries;
16
+ this.entries = {};
17
+ this.onCollect(copy, this.resp);
18
+ }
19
+
20
+ _addByKey(key, data) {
21
+ if (!this.entries[key]) {
22
+ this.entries[key] = [];
23
+ }
24
+ this.entries[key] = this.entries[key].concat(data);
25
+ }
26
+
27
+ grab(key, data) {
28
+ this._addByKey(key, data);
29
+ this.release();
30
+ }
31
+ }
32
+
33
+ module.exports = ArrayCollector;
package/lib/async.js CHANGED
@@ -1,34 +1,34 @@
1
- 'use strict';
2
-
3
- const watt = require('gigawatts');
4
-
5
- class Async {
6
- constructor() {
7
- watt.wrapAll(this);
8
- }
9
-
10
- /**
11
- * Reduce an array to a map with an async iteration.
12
- *
13
- * It's useful in the case where the main event loop must not
14
- * be blocked and the array is very large. The purpose of this
15
- * helper is only for task which be done with an usual sync
16
- * map reduce.
17
- *
18
- * @param {*} keyFunc - Key for the item in the map.
19
- * @param {*} valueFunc - Value for the item in the map.
20
- * @param {*} list - The input array.
21
- * @returns {Object} the map.
22
- */
23
- *mapReduce(keyFunc, valueFunc, list) {
24
- const state = {};
25
- for (const item of list) {
26
- state[keyFunc(item)] = yield new Promise((resolve) =>
27
- resolve(valueFunc(item))
28
- );
29
- }
30
- return state;
31
- }
32
- }
33
-
34
- module.exports = new Async();
1
+ 'use strict';
2
+
3
+ const watt = require('gigawatts');
4
+
5
+ class Async {
6
+ constructor() {
7
+ watt.wrapAll(this);
8
+ }
9
+
10
+ /**
11
+ * Reduce an array to a map with an async iteration.
12
+ *
13
+ * It's useful in the case where the main event loop must not
14
+ * be blocked and the array is very large. The purpose of this
15
+ * helper is only for task which be done with an usual sync
16
+ * map reduce.
17
+ *
18
+ * @param {*} keyFunc - Key for the item in the map.
19
+ * @param {*} valueFunc - Value for the item in the map.
20
+ * @param {*} list - The input array.
21
+ * @returns {Object} the map.
22
+ */
23
+ *mapReduce(keyFunc, valueFunc, list) {
24
+ const state = {};
25
+ for (const item of list) {
26
+ state[keyFunc(item)] = yield new Promise((resolve) =>
27
+ resolve(valueFunc(item))
28
+ );
29
+ }
30
+ return state;
31
+ }
32
+ }
33
+
34
+ module.exports = new Async();
package/lib/batch.js CHANGED
@@ -1,24 +1,24 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const xFs = require('xcraft-core-fs');
6
-
7
- exports.run = function (filter, location, callbackAction) {
8
- var files = xFs.ls(location);
9
-
10
- files.forEach(function (file) {
11
- var fullPath = path.join(location, file);
12
- var st = fs.lstatSync(fullPath);
13
-
14
- if (st.isDirectory()) {
15
- exports.run(filter, fullPath, callbackAction);
16
- return;
17
- }
18
-
19
- if (!filter || filter.test(file)) {
20
- callbackAction(fullPath);
21
- return;
22
- }
23
- });
24
- };
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const xFs = require('xcraft-core-fs');
6
+
7
+ exports.run = function (filter, location, callbackAction) {
8
+ var files = xFs.ls(location);
9
+
10
+ files.forEach(function (file) {
11
+ var fullPath = path.join(location, file);
12
+ var st = fs.lstatSync(fullPath);
13
+
14
+ if (st.isDirectory()) {
15
+ exports.run(filter, fullPath, callbackAction);
16
+ return;
17
+ }
18
+
19
+ if (!filter || filter.test(file)) {
20
+ callbackAction(fullPath);
21
+ return;
22
+ }
23
+ });
24
+ };
package/lib/crypto.js CHANGED
@@ -1,92 +1,92 @@
1
- 'use strict';
2
-
3
- var crypto = require('crypto');
4
- const {v4: uuidV4} = require('uuid');
5
-
6
- exports.md5 = function (data) {
7
- var md5 = crypto.createHash('md5');
8
- md5.update(data);
9
- return md5.digest('hex');
10
- };
11
-
12
- exports.sha256 = function (data) {
13
- var sha = crypto.createHash('sha256');
14
- sha.update(data);
15
- return sha.digest('hex');
16
- };
17
-
18
- exports.genToken = function () {
19
- return uuidV4().replace(/-/g, '');
20
- };
21
-
22
- // Get the number of random bytes needed and the mask
23
- // used to generate an integer in the specified range
24
- function getBytesSizeAndMask(range) {
25
- let mask = 0;
26
- let bitsCount = 0;
27
- while (range > 0) {
28
- mask = (mask << 1) | 1; // 0x00000011 -> 0x00000111
29
- range = range >>> 1; // 0x00111100 -> 0x00011110
30
- bitsCount++;
31
- }
32
- const bytesSize = (bitsCount + 7) >> 3; // ceil(bitsCount / 8)
33
- return {bytesSize, mask};
34
- }
35
-
36
- /**
37
- * Generate a random integer between min and max.
38
- *
39
- * @param {number} min - Min value.
40
- * @param {number} max - Max value.
41
- * @returns {number} A random integer between min and max
42
- */
43
- function randomInt(min, max) {
44
- const range = max - min;
45
- if (range > 2147483647 || range < 0) {
46
- // max safe integer for bitwise operations (2147483647 = 2^31 - 1)
47
- throw new Error('Range must be between 0 and 2147483647');
48
- }
49
-
50
- const {bytesSize, mask} = getBytesSizeAndMask(range);
51
- let randomInt;
52
- do {
53
- const randomBytes = crypto.randomBytes(bytesSize);
54
-
55
- // Convert random bytes to an integer
56
- randomInt = 0;
57
- for (let i = 0; i < bytesSize; i++) {
58
- randomInt = (randomInt << 8) | randomBytes[i];
59
- }
60
-
61
- // Apply mask to reduce the number of discarded numbers
62
- randomInt = randomInt & mask;
63
- } while (randomInt > range); // Discard random numbers outside the range
64
-
65
- return min + randomInt;
66
- }
67
- exports.randomInt = randomInt;
68
-
69
- const defaultChars =
70
- 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ@+-=*&%?!_23456789';
71
-
72
- function randomChar(chars = defaultChars) {
73
- const charId = randomInt(0, chars.length - 1);
74
- return chars[charId];
75
- }
76
- exports.randomChar = randomChar;
77
-
78
- /**
79
- * Generate a random password.
80
- *
81
- * @param {number} [length] - The password length.
82
- * @param {string} [chars] - A string with the chars to use for password generation.
83
- * @returns {string} A random string
84
- */
85
- exports.randomPassword = function (length = 12, chars) {
86
- let password = '';
87
- for (let i = 0; i < length; i++) {
88
- const char = randomChar(chars);
89
- password += char;
90
- }
91
- return password;
92
- };
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+ const {v4: uuidV4} = require('uuid');
5
+
6
+ exports.md5 = function (data) {
7
+ var md5 = crypto.createHash('md5');
8
+ md5.update(data);
9
+ return md5.digest('hex');
10
+ };
11
+
12
+ exports.sha256 = function (data) {
13
+ var sha = crypto.createHash('sha256');
14
+ sha.update(data);
15
+ return sha.digest('hex');
16
+ };
17
+
18
+ exports.genToken = function () {
19
+ return uuidV4().replace(/-/g, '');
20
+ };
21
+
22
+ // Get the number of random bytes needed and the mask
23
+ // used to generate an integer in the specified range
24
+ function getBytesSizeAndMask(range) {
25
+ let mask = 0;
26
+ let bitsCount = 0;
27
+ while (range > 0) {
28
+ mask = (mask << 1) | 1; // 0x00000011 -> 0x00000111
29
+ range = range >>> 1; // 0x00111100 -> 0x00011110
30
+ bitsCount++;
31
+ }
32
+ const bytesSize = (bitsCount + 7) >> 3; // ceil(bitsCount / 8)
33
+ return {bytesSize, mask};
34
+ }
35
+
36
+ /**
37
+ * Generate a random integer between min and max.
38
+ *
39
+ * @param {number} min - Min value.
40
+ * @param {number} max - Max value.
41
+ * @returns {number} A random integer between min and max
42
+ */
43
+ function randomInt(min, max) {
44
+ const range = max - min;
45
+ if (range > 2147483647 || range < 0) {
46
+ // max safe integer for bitwise operations (2147483647 = 2^31 - 1)
47
+ throw new Error('Range must be between 0 and 2147483647');
48
+ }
49
+
50
+ const {bytesSize, mask} = getBytesSizeAndMask(range);
51
+ let randomInt;
52
+ do {
53
+ const randomBytes = crypto.randomBytes(bytesSize);
54
+
55
+ // Convert random bytes to an integer
56
+ randomInt = 0;
57
+ for (let i = 0; i < bytesSize; i++) {
58
+ randomInt = (randomInt << 8) | randomBytes[i];
59
+ }
60
+
61
+ // Apply mask to reduce the number of discarded numbers
62
+ randomInt = randomInt & mask;
63
+ } while (randomInt > range); // Discard random numbers outside the range
64
+
65
+ return min + randomInt;
66
+ }
67
+ exports.randomInt = randomInt;
68
+
69
+ const defaultChars =
70
+ 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ@+-=*&%?!_23456789';
71
+
72
+ function randomChar(chars = defaultChars) {
73
+ const charId = randomInt(0, chars.length - 1);
74
+ return chars[charId];
75
+ }
76
+ exports.randomChar = randomChar;
77
+
78
+ /**
79
+ * Generate a random password.
80
+ *
81
+ * @param {number} [length] - The password length.
82
+ * @param {string} [chars] - A string with the chars to use for password generation.
83
+ * @returns {string} A random string
84
+ */
85
+ exports.randomPassword = function (length = 12, chars) {
86
+ let password = '';
87
+ for (let i = 0; i < length; i++) {
88
+ const char = randomChar(chars);
89
+ password += char;
90
+ }
91
+ return password;
92
+ };
package/lib/cursorPump.js CHANGED
@@ -1,29 +1,29 @@
1
- 'use strict';
2
-
3
- const watt = require('gigawatts');
4
-
5
- class CursorPump {
6
- constructor(cursor) {
7
- this.cursor = cursor;
8
- watt.wrapAll(this);
9
- }
10
-
11
- *toArray() {
12
- let run = true;
13
- let results = [];
14
- do {
15
- try {
16
- const row = yield this.cursor.next();
17
- results.push(row);
18
- } catch {
19
- run = false;
20
- }
21
- } while (run);
22
- return results;
23
- }
24
-
25
- *pump() {
26
- return yield this.cursor.next();
27
- }
28
- }
29
- module.exports = CursorPump;
1
+ 'use strict';
2
+
3
+ const watt = require('gigawatts');
4
+
5
+ class CursorPump {
6
+ constructor(cursor) {
7
+ this.cursor = cursor;
8
+ watt.wrapAll(this);
9
+ }
10
+
11
+ *toArray() {
12
+ let run = true;
13
+ let results = [];
14
+ do {
15
+ try {
16
+ const row = yield this.cursor.next();
17
+ results.push(row);
18
+ } catch {
19
+ run = false;
20
+ }
21
+ } while (run);
22
+ return results;
23
+ }
24
+
25
+ *pump() {
26
+ return yield this.cursor.next();
27
+ }
28
+ }
29
+ module.exports = CursorPump;
@@ -1,21 +1,21 @@
1
- 'use strict';
2
-
3
- const {debounce} = require('lodash');
4
-
5
- class EventDebouncer {
6
- constructor(resp, wait = 1000) {
7
- this.debouncers = {};
8
- this.wait = wait;
9
- this.resp = resp;
10
- }
11
-
12
- publish(topic, data) {
13
- if (!this.debouncers[topic]) {
14
- const send = (topic, data) => this.resp.events.send(topic, data);
15
- this.debouncers[topic] = debounce(send, this.wait);
16
- }
17
- this.debouncers[topic](topic, data);
18
- }
19
- }
20
-
21
- module.exports = EventDebouncer;
1
+ 'use strict';
2
+
3
+ const {debounce} = require('lodash');
4
+
5
+ class EventDebouncer {
6
+ constructor(resp, wait = 1000) {
7
+ this.debouncers = {};
8
+ this.wait = wait;
9
+ this.resp = resp;
10
+ }
11
+
12
+ publish(topic, data) {
13
+ if (!this.debouncers[topic]) {
14
+ const send = (topic, data) => this.resp.events.send(topic, data);
15
+ this.debouncers[topic] = debounce(send, this.wait);
16
+ }
17
+ this.debouncers[topic](topic, data);
18
+ }
19
+ }
20
+
21
+ module.exports = EventDebouncer;