aneka 0.17.6 → 0.17.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.
Files changed (147) hide show
  1. package/.jsdoc.conf.json +0 -0
  2. package/CHANGES.md +87 -83
  3. package/LICENSE +21 -21
  4. package/README.md +63 -63
  5. package/docs/arrange-array.js.html +0 -0
  6. package/docs/create-class.js.html +0 -0
  7. package/docs/current-loc.js.html +0 -0
  8. package/docs/data/search.json +0 -0
  9. package/docs/defaults-deep.js.html +0 -0
  10. package/docs/delay.js.html +0 -0
  11. package/docs/extract-text.js.html +0 -0
  12. package/docs/fill-object.js.html +0 -0
  13. package/docs/filter-duplicate.js.html +0 -0
  14. package/docs/filter-index.js.html +0 -0
  15. package/docs/fonts/Inconsolata-Regular.ttf +0 -0
  16. package/docs/fonts/OpenSans-Regular.ttf +0 -0
  17. package/docs/fonts/WorkSans-Bold.ttf +0 -0
  18. package/docs/format-text.js.html +0 -0
  19. package/docs/generate-id.js.html +0 -0
  20. package/docs/get-caller-filename.js.html +0 -0
  21. package/docs/get-global-module-dir.js.html +0 -0
  22. package/docs/get-index.js.html +0 -0
  23. package/docs/get-key-by-value.js.html +0 -0
  24. package/docs/global.html +0 -0
  25. package/docs/includes.js.html +0 -0
  26. package/docs/index.html +0 -0
  27. package/docs/is-class.js.html +0 -0
  28. package/docs/is-set.js.html +0 -0
  29. package/docs/merge-objects-by-key.js.html +0 -0
  30. package/docs/paginate.js.html +0 -0
  31. package/docs/parse-args-argv.js.html +0 -0
  32. package/docs/parse-duration.js.html +0 -0
  33. package/docs/parse-env.js.html +0 -0
  34. package/docs/parse-kv-string.js.html +0 -0
  35. package/docs/parse-object.js.html +0 -0
  36. package/docs/pascal-case.js.html +0 -0
  37. package/docs/random-range.js.html +0 -0
  38. package/docs/resolve-path.js.html +0 -0
  39. package/docs/round.js.html +0 -0
  40. package/docs/scripts/core.js +0 -0
  41. package/docs/scripts/core.min.js +0 -0
  42. package/docs/scripts/resize.js +0 -0
  43. package/docs/scripts/search.js +0 -0
  44. package/docs/scripts/search.min.js +5 -5
  45. package/docs/scripts/third-party/Apache-License-2.0.txt +0 -0
  46. package/docs/scripts/third-party/fuse.js +0 -0
  47. package/docs/scripts/third-party/hljs-line-num-original.js +0 -0
  48. package/docs/scripts/third-party/hljs-line-num.js +0 -0
  49. package/docs/scripts/third-party/hljs-original.js +0 -0
  50. package/docs/scripts/third-party/hljs.js +0 -0
  51. package/docs/scripts/third-party/popper.js +0 -0
  52. package/docs/scripts/third-party/tippy.js +0 -0
  53. package/docs/scripts/third-party/tocbot.js +0 -0
  54. package/docs/scripts/third-party/tocbot.min.js +0 -0
  55. package/docs/sec-to-hms.js.html +0 -0
  56. package/docs/set-immediate.js.html +0 -0
  57. package/docs/set-interval.js.html +0 -0
  58. package/docs/set-timeout.js.html +0 -0
  59. package/docs/shim.js.html +0 -0
  60. package/docs/static/bitcoin.jpeg +0 -0
  61. package/docs/styles/clean-jsdoc-theme-base.css +0 -0
  62. package/docs/styles/clean-jsdoc-theme-dark.css +0 -0
  63. package/docs/styles/clean-jsdoc-theme-light.css +0 -0
  64. package/docs/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  65. package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  66. package/docs/styles/clean-jsdoc-theme.min.css +0 -0
  67. package/docs/text-to-array.js.html +0 -0
  68. package/docs/titleize.js.html +0 -0
  69. package/docs/without.js.html +0 -0
  70. package/index.js +119 -119
  71. package/package.json +48 -48
  72. package/src/arrange-array.js +25 -25
  73. package/src/create-class.js +35 -35
  74. package/src/current-loc.js +0 -0
  75. package/src/defaults-deep.js +20 -20
  76. package/src/delay.js +19 -19
  77. package/src/extract-text.js +23 -23
  78. package/src/fill-object.js +17 -17
  79. package/src/filter-duplicate.js +26 -26
  80. package/src/filter-index.js +17 -17
  81. package/src/format-text.js +88 -88
  82. package/src/generate-id.js +35 -35
  83. package/src/get-caller-filename.js +25 -25
  84. package/src/get-global-module-dir.js +24 -20
  85. package/src/get-index.js +15 -15
  86. package/src/get-key-by-value.js +12 -12
  87. package/src/includes.js +18 -18
  88. package/src/is-class.js +13 -13
  89. package/src/is-set.js +11 -11
  90. package/src/log.js +19 -19
  91. package/src/merge-objects-by-key.js +24 -24
  92. package/src/paginate.js +45 -45
  93. package/src/parse-args-argv.js +0 -0
  94. package/src/parse-duration.js +14 -14
  95. package/src/parse-env.js +0 -0
  96. package/src/parse-kv-string.js +21 -21
  97. package/src/parse-object.js +76 -76
  98. package/src/pascal-case.js +14 -14
  99. package/src/random-range.js +15 -15
  100. package/src/resolve-path.js +0 -0
  101. package/src/round.js +13 -13
  102. package/src/sec-to-hms.js +27 -27
  103. package/src/set-immediate.js +12 -12
  104. package/src/set-interval.js +56 -56
  105. package/src/set-timeout.js +34 -34
  106. package/src/shim.js +28 -28
  107. package/src/text-to-array.js +17 -17
  108. package/src/titleize.js +37 -37
  109. package/src/without.js +18 -18
  110. package/test/arrange-array.js +41 -41
  111. package/test/create-class.js +33 -33
  112. package/test/current-loc.js +24 -24
  113. package/test/defaults-deep.js +19 -19
  114. package/test/delay.js +17 -17
  115. package/test/extract-text.js +17 -17
  116. package/test/fill-object.js +17 -17
  117. package/test/filter-duplicate.js +19 -19
  118. package/test/filter-index.js +17 -17
  119. package/test/format-text.js +26 -26
  120. package/test/generate-id.js +28 -28
  121. package/test/get-caller-filename.js +22 -22
  122. package/test/get-global-module-dir.js +51 -51
  123. package/test/get-index.js +17 -17
  124. package/test/get-key-by-value.js +17 -17
  125. package/test/includes.js +18 -18
  126. package/test/is-class.js +19 -19
  127. package/test/is-set.js +21 -21
  128. package/test/log.js +47 -47
  129. package/test/merge-objects-by-key.js +19 -19
  130. package/test/paginate.js +24 -24
  131. package/test/parse-args-argv.js +63 -63
  132. package/test/parse-duration.js +18 -18
  133. package/test/parse-env.js +35 -35
  134. package/test/parse-kv-string.js +17 -17
  135. package/test/parse-object.js +30 -30
  136. package/test/pascal-case.js +18 -18
  137. package/test/random-range.js +21 -21
  138. package/test/resolve-path.js +22 -22
  139. package/test/round.js +19 -19
  140. package/test/sec-to-hms.js +18 -18
  141. package/test/set-immediate.js +17 -17
  142. package/test/set-interval.js +196 -196
  143. package/test/set-timeout.js +120 -120
  144. package/test/shim.js +34 -34
  145. package/test/text-to-array.js +18 -18
  146. package/test/titleize.js +27 -27
  147. package/test/without.js +17 -17
package/src/paginate.js CHANGED
@@ -1,45 +1,45 @@
1
- import isPlainObject from 'lodash-es/isPlainObject.js'
2
- import forOwn from 'lodash-es/forOwn.js'
3
- import isEmpty from 'lodash-es/isEmpty.js'
4
- import orderBy from 'lodash-es/orderBy.js'
5
-
6
- /**
7
- * Paginates and optionally sorts a collection.
8
- *
9
- * @param {Array<Object>} [collection=[]] Source collection.
10
- * @param {Object} [options={}] Pagination options.
11
- * @param {number} [options.page=1] 1-based page number.
12
- * @param {number} [options.limit=25] Items per page.
13
- * @param {Object<string, number>} [options.sort] Sort descriptor where value < 0 means descending.
14
- * @returns {Object} Paginated result.
15
- * @returns {Array<Object>} return.data Sliced page data.
16
- * @returns {number} return.page Current page number.
17
- * @returns {number} return.limit Page size.
18
- * @returns {number} return.count Total items in source collection.
19
- * @returns {number} return.pages Total page count.
20
- */
21
- function paginate (collection = [], options = {}) {
22
- const { page = 1, limit = 25, sort } = options
23
- const count = collection.length
24
- const offset = (page - 1) * limit
25
- const fields = []
26
- const dirs = []
27
- if (isPlainObject(sort)) {
28
- forOwn(sort, (v, k) => {
29
- fields.push(k)
30
- dirs.push(v < 0 ? 'desc' : 'asc')
31
- })
32
- }
33
- if (!isEmpty(fields)) collection = orderBy(collection, fields, dirs)
34
- const data = collection.slice(offset, offset + limit)
35
-
36
- return {
37
- data,
38
- page,
39
- limit,
40
- count,
41
- pages: Math.ceil(collection.length / limit)
42
- }
43
- }
44
-
45
- export default paginate
1
+ import isPlainObject from 'lodash-es/isPlainObject.js'
2
+ import forOwn from 'lodash-es/forOwn.js'
3
+ import isEmpty from 'lodash-es/isEmpty.js'
4
+ import orderBy from 'lodash-es/orderBy.js'
5
+
6
+ /**
7
+ * Paginates and optionally sorts a collection.
8
+ *
9
+ * @param {Array<Object>} [collection=[]] Source collection.
10
+ * @param {Object} [options={}] Pagination options.
11
+ * @param {number} [options.page=1] 1-based page number.
12
+ * @param {number} [options.limit=25] Items per page.
13
+ * @param {Object<string, number>} [options.sort] Sort descriptor where value < 0 means descending.
14
+ * @returns {Object} Paginated result.
15
+ * @returns {Array<Object>} return.data Sliced page data.
16
+ * @returns {number} return.page Current page number.
17
+ * @returns {number} return.limit Page size.
18
+ * @returns {number} return.count Total items in source collection.
19
+ * @returns {number} return.pages Total page count.
20
+ */
21
+ function paginate (collection = [], options = {}) {
22
+ const { page = 1, limit = 25, sort } = options
23
+ const count = collection.length
24
+ const offset = (page - 1) * limit
25
+ const fields = []
26
+ const dirs = []
27
+ if (isPlainObject(sort)) {
28
+ forOwn(sort, (v, k) => {
29
+ fields.push(k)
30
+ dirs.push(v < 0 ? 'desc' : 'asc')
31
+ })
32
+ }
33
+ if (!isEmpty(fields)) collection = orderBy(collection, fields, dirs)
34
+ const data = collection.slice(offset, offset + limit)
35
+
36
+ return {
37
+ data,
38
+ page,
39
+ limit,
40
+ count,
41
+ pages: Math.ceil(collection.length / limit)
42
+ }
43
+ }
44
+
45
+ export default paginate
File without changes
@@ -1,14 +1,14 @@
1
- import { isNumber } from 'lodash-es'
2
- import ms from 'ms'
3
- /**
4
- * Parse duration to its millisecond value. Use {@link https://github.com/vercel/ms|ms} under the hood
5
- *
6
- * @param {(number|string)} dur - If string is given, parse this to its millisecond value. Otherwise returns as is
7
- * @returns {number}
8
- * @see {@link https://github.com/vercel/ms|ms}
9
- */
10
- function parseDuration (dur) {
11
- return isNumber(dur) ? dur : ms(dur)
12
- }
13
-
14
- export default parseDuration
1
+ import { isNumber } from 'lodash-es'
2
+ import ms from 'ms'
3
+ /**
4
+ * Parse duration to its millisecond value. Use {@link https://github.com/vercel/ms|ms} under the hood
5
+ *
6
+ * @param {(number|string)} dur - If string is given, parse this to its millisecond value. Otherwise returns as is
7
+ * @returns {number}
8
+ * @see {@link https://github.com/vercel/ms|ms}
9
+ */
10
+ function parseDuration (dur) {
11
+ return isNumber(dur) ? dur : ms(dur)
12
+ }
13
+
14
+ export default parseDuration
package/src/parse-env.js CHANGED
File without changes
@@ -1,21 +1,21 @@
1
- import dotenvParseVariables from 'dotenv-parse-variables'
2
-
3
- /**
4
- * Parses key-value text into an object and normalizes primitive types.
5
- *
6
- * @param {string} text Input key-value string.
7
- * @param {boolean} [trimValue=true] Trim value parts.
8
- * @param {string} [lineDelimiter='|'] Delimiter between lines.
9
- * @returns {Object} Parsed object.
10
- */
11
- function parseKvString (text, trimValue = true, lineDelimiter = '|') {
12
- const lines = text.split(lineDelimiter).join('\n').split('\n')
13
- const item = {}
14
- for (const line of lines) {
15
- const [k, v] = line.split('=')
16
- item[k.trim()] = trimValue ? v.trim() : v
17
- }
18
- return dotenvParseVariables(item, { assignToProcessEnv: false })
19
- }
20
-
21
- export default parseKvString
1
+ import dotenvParseVariables from 'dotenv-parse-variables'
2
+
3
+ /**
4
+ * Parses key-value text into an object and normalizes primitive types.
5
+ *
6
+ * @param {string} text Input key-value string.
7
+ * @param {boolean} [trimValue=true] Trim value parts.
8
+ * @param {string} [lineDelimiter='|'] Delimiter between lines.
9
+ * @returns {Object} Parsed object.
10
+ */
11
+ function parseKvString (text, trimValue = true, lineDelimiter = '|') {
12
+ const lines = text.split(lineDelimiter).join('\n').split('\n')
13
+ const item = {}
14
+ for (const line of lines) {
15
+ const [k, v] = line.split('=')
16
+ item[k.trim()] = trimValue ? v.trim() : v
17
+ }
18
+ return dotenvParseVariables(item, { assignToProcessEnv: false })
19
+ }
20
+
21
+ export default parseKvString
@@ -1,76 +1,76 @@
1
- import isSet from './is-set.js'
2
- import parseDuration from './parse-duration.js'
3
- import dotenvParseVariables from 'dotenv-parse-variables'
4
- import { set, isString, cloneDeep, isPlainObject, isArray, omit } from 'lodash-es'
5
-
6
- /**
7
- * Parses an object (or JSON string) and recursively normalizes nested values.
8
- *
9
- * When `options.parseValue` is enabled, string values are converted using
10
- * dotenv-parse-variables rules (for example booleans, numbers, arrays, and null),
11
- * including values inside arrays and nested objects.
12
- *
13
- * Special key handling:
14
- * - Keys ending with `Dur` are converted to milliseconds using parseDuration.
15
- * - Keys ending with `Dt` are converted to Unix timestamps using Date.parse.
16
- * - `options.translator` can translate prefixed keys/values before normalization.
17
- *
18
- * @param {(Object|string)} input If string is given, parse it first using JSON.parse.
19
- * @param {Object} [options={}] Options.
20
- * @param {boolean} [options.silent=true] If `true`, parsing errors are ignored and values become undefined.
21
- * @param {boolean} [options.parseValue=false] If `true`, values are parsed and normalized.
22
- * @param {Object} [options.translator] Optional translator config for prefixed keys/values.
23
- * @returns {Object} Parsed and normalized object.
24
- * @see {@link https://github.com/ladjs/dotenv-parse-variables}
25
- */
26
- function parseObject (input, options = {}) {
27
- const { silent = true, parseValue = false, translator = {} } = options
28
- const { prefix, lang, handler } = translator
29
- const statics = ['*']
30
- if (isString(input)) {
31
- try {
32
- input = JSON.parse(input)
33
- } catch (err) {
34
- if (silent) input = {}
35
- else throw err
36
- }
37
- }
38
- let obj = cloneDeep(input)
39
- const keys = Object.keys(obj)
40
- const mutated = []
41
- keys.forEach(k => {
42
- let v = obj[k]
43
- if (isPlainObject(v)) obj[k] = parseObject(v, options)
44
- else if (isArray(v)) {
45
- v.forEach((i, idx) => {
46
- if (isPlainObject(i)) obj[k][idx] = parseObject(i, options)
47
- else if (statics.includes(i)) obj[k][idx] = i
48
- else if (parseValue) obj[k][idx] = dotenvParseVariables(set({}, 'item', obj[k][idx]), { assignToProcessEnv: false }).item
49
- if (isArray(obj[k][idx])) obj[k][idx] = obj[k][idx].map(item => typeof item === 'string' ? item.trim() : item)
50
- })
51
- } else if (isSet(v)) {
52
- if (isString(v) && prefix && lang && handler && v.startsWith(prefix)) v = handler(v.slice(prefix.length))
53
- try {
54
- if (statics.includes(v)) obj[k] = v
55
- else if (isString(v) && prefix && handler && k.startsWith(prefix)) {
56
- const newK = k.slice(prefix.length)
57
- if (lang) obj[newK] = handler(v)
58
- else obj[newK] = v
59
- mutated.push(k)
60
- } else if (parseValue) {
61
- obj[k] = dotenvParseVariables(set({}, 'item', v), { assignToProcessEnv: false }).item
62
- if (isArray(obj[k])) obj[k] = obj[k].map(item => typeof item === 'string' ? item.trim() : item)
63
- }
64
- if (k.slice(-3) === 'Dur') obj[k] = parseDuration(v)
65
- if (k.slice(-2) === 'Dt') obj[k] = Date.parse(v)
66
- } catch (err) {
67
- obj[k] = undefined
68
- if (!silent) throw err
69
- }
70
- }
71
- })
72
- if (mutated.length > 0) obj = omit(obj, mutated)
73
- return obj
74
- }
75
-
76
- export default parseObject
1
+ import isSet from './is-set.js'
2
+ import parseDuration from './parse-duration.js'
3
+ import dotenvParseVariables from 'dotenv-parse-variables'
4
+ import { set, isString, cloneDeep, isPlainObject, isArray, omit } from 'lodash-es'
5
+
6
+ /**
7
+ * Parses an object (or JSON string) and recursively normalizes nested values.
8
+ *
9
+ * When `options.parseValue` is enabled, string values are converted using
10
+ * dotenv-parse-variables rules (for example booleans, numbers, arrays, and null),
11
+ * including values inside arrays and nested objects.
12
+ *
13
+ * Special key handling:
14
+ * - Keys ending with `Dur` are converted to milliseconds using parseDuration.
15
+ * - Keys ending with `Dt` are converted to Unix timestamps using Date.parse.
16
+ * - `options.translator` can translate prefixed keys/values before normalization.
17
+ *
18
+ * @param {(Object|string)} input If string is given, parse it first using JSON.parse.
19
+ * @param {Object} [options={}] Options.
20
+ * @param {boolean} [options.silent=true] If `true`, parsing errors are ignored and values become undefined.
21
+ * @param {boolean} [options.parseValue=false] If `true`, values are parsed and normalized.
22
+ * @param {Object} [options.translator] Optional translator config for prefixed keys/values.
23
+ * @returns {Object} Parsed and normalized object.
24
+ * @see {@link https://github.com/ladjs/dotenv-parse-variables}
25
+ */
26
+ function parseObject (input, options = {}) {
27
+ const { silent = true, parseValue = false, translator = {} } = options
28
+ const { prefix, lang, handler } = translator
29
+ const statics = ['*']
30
+ if (isString(input)) {
31
+ try {
32
+ input = JSON.parse(input)
33
+ } catch (err) {
34
+ if (silent) input = {}
35
+ else throw err
36
+ }
37
+ }
38
+ let obj = cloneDeep(input)
39
+ const keys = Object.keys(obj)
40
+ const mutated = []
41
+ keys.forEach(k => {
42
+ let v = obj[k]
43
+ if (isPlainObject(v)) obj[k] = parseObject(v, options)
44
+ else if (isArray(v)) {
45
+ v.forEach((i, idx) => {
46
+ if (isPlainObject(i)) obj[k][idx] = parseObject(i, options)
47
+ else if (statics.includes(i)) obj[k][idx] = i
48
+ else if (parseValue) obj[k][idx] = dotenvParseVariables(set({}, 'item', obj[k][idx]), { assignToProcessEnv: false }).item
49
+ if (isArray(obj[k][idx])) obj[k][idx] = obj[k][idx].map(item => typeof item === 'string' ? item.trim() : item)
50
+ })
51
+ } else if (isSet(v)) {
52
+ if (isString(v) && prefix && lang && handler && v.startsWith(prefix)) v = handler(v.slice(prefix.length))
53
+ try {
54
+ if (statics.includes(v)) obj[k] = v
55
+ else if (isString(v) && prefix && handler && k.startsWith(prefix)) {
56
+ const newK = k.slice(prefix.length)
57
+ if (lang) obj[newK] = handler(v)
58
+ else obj[newK] = v
59
+ mutated.push(k)
60
+ } else if (parseValue) {
61
+ obj[k] = dotenvParseVariables(set({}, 'item', v), { assignToProcessEnv: false }).item
62
+ if (isArray(obj[k])) obj[k] = obj[k].map(item => typeof item === 'string' ? item.trim() : item)
63
+ }
64
+ if (k.slice(-3) === 'Dur') obj[k] = parseDuration(v)
65
+ if (k.slice(-2) === 'Dt') obj[k] = Date.parse(v)
66
+ } catch (err) {
67
+ obj[k] = undefined
68
+ if (!silent) throw err
69
+ }
70
+ }
71
+ })
72
+ if (mutated.length > 0) obj = omit(obj, mutated)
73
+ return obj
74
+ }
75
+
76
+ export default parseObject
@@ -1,14 +1,14 @@
1
- import upperFirst from 'lodash-es/upperFirst.js'
2
- import camelCase from 'lodash-es/camelCase.js'
3
-
4
- /**
5
- * Converts text into PascalCase.
6
- *
7
- * @param {string} text Source text.
8
- * @returns {string} Pascal-cased text.
9
- */
10
- function pascalCase (text) {
11
- return upperFirst(camelCase(text))
12
- }
13
-
14
- export default pascalCase
1
+ import upperFirst from 'lodash-es/upperFirst.js'
2
+ import camelCase from 'lodash-es/camelCase.js'
3
+
4
+ /**
5
+ * Converts text into PascalCase.
6
+ *
7
+ * @param {string} text Source text.
8
+ * @returns {string} Pascal-cased text.
9
+ */
10
+ function pascalCase (text) {
11
+ return upperFirst(camelCase(text))
12
+ }
13
+
14
+ export default pascalCase
@@ -1,15 +1,15 @@
1
- /**
2
- * Generate random value.
3
- *
4
- * @param {number} min Minimum value (inclusive).
5
- * @param {number} max Maximum value (inclusive).
6
- * @param {boolean} alpha Set ```true``` to return alphanumeric characters.
7
- * @returns {number|string} Random value.
8
- */
9
- function randomRange (min = 0, max = 65439, alpha = false) {
10
- const num = Math.floor(Math.random() * (max - min + 1) + min)
11
- if (!alpha) return num
12
- return String.fromCharCode(96 + num)
13
- }
14
-
15
- export default randomRange
1
+ /**
2
+ * Generate random value.
3
+ *
4
+ * @param {number} min Minimum value (inclusive).
5
+ * @param {number} max Maximum value (inclusive).
6
+ * @param {boolean} alpha Set ```true``` to return alphanumeric characters.
7
+ * @returns {number|string} Random value.
8
+ */
9
+ function randomRange (min = 0, max = 65439, alpha = false) {
10
+ const num = Math.floor(Math.random() * (max - min + 1) + min)
11
+ if (!alpha) return num
12
+ return String.fromCharCode(96 + num)
13
+ }
14
+
15
+ export default randomRange
File without changes
package/src/round.js CHANGED
@@ -1,13 +1,13 @@
1
- /**
2
- * Rounds a number using decimal precision.
3
- *
4
- * @param {number} val Number to round.
5
- * @param {number} [scale=0] Decimal places.
6
- * @returns {number} Rounded number.
7
- */
8
- function round (val, scale = 0) {
9
- scale = scale <= 0 ? 1 : 10 ** scale
10
- return Math.round(val * scale) / scale
11
- }
12
-
13
- export default round
1
+ /**
2
+ * Rounds a number using decimal precision.
3
+ *
4
+ * @param {number} val Number to round.
5
+ * @param {number} [scale=0] Decimal places.
6
+ * @returns {number} Rounded number.
7
+ */
8
+ function round (val, scale = 0) {
9
+ scale = scale <= 0 ? 1 : 10 ** scale
10
+ return Math.round(val * scale) / scale
11
+ }
12
+
13
+ export default round
package/src/sec-to-hms.js CHANGED
@@ -1,27 +1,27 @@
1
- import padStart from 'lodash-es/padStart.js'
2
-
3
- /**
4
- * Converts seconds (or milliseconds) to HH:MM:SS string.
5
- *
6
- * @param {number|string} secs Duration value.
7
- * @param {boolean} [ms] Treat input as milliseconds when true.
8
- * @returns {string} Formatted duration.
9
- */
10
- export default function secToHms (secs, ms) {
11
- let remain
12
- if (ms) {
13
- remain = secs % 1000
14
- secs = Math.floor(secs / 1000)
15
- }
16
- const secNum = parseInt(secs, 10)
17
- const hours = Math.floor(secNum / 3600)
18
- const minutes = Math.floor(secNum / 60) % 60
19
- const seconds = secNum % 60
20
-
21
- let hms = [hours, minutes, seconds]
22
- .map(v => v < 10 ? '0' + v : v)
23
- .filter((v, i) => v !== '00' || i > 0)
24
- .join(':')
25
- if (ms) hms += '+' + padStart(remain, 3, '0')
26
- return hms
27
- }
1
+ import padStart from 'lodash-es/padStart.js'
2
+
3
+ /**
4
+ * Converts seconds (or milliseconds) to HH:MM:SS string.
5
+ *
6
+ * @param {number|string} secs Duration value.
7
+ * @param {boolean} [ms] Treat input as milliseconds when true.
8
+ * @returns {string} Formatted duration.
9
+ */
10
+ export default function secToHms (secs, ms) {
11
+ let remain
12
+ if (ms) {
13
+ remain = secs % 1000
14
+ secs = Math.floor(secs / 1000)
15
+ }
16
+ const secNum = parseInt(secs, 10)
17
+ const hours = Math.floor(secNum / 3600)
18
+ const minutes = Math.floor(secNum / 60) % 60
19
+ const seconds = secNum % 60
20
+
21
+ let hms = [hours, minutes, seconds]
22
+ .map(v => v < 10 ? '0' + v : v)
23
+ .filter((v, i) => v !== '00' || i > 0)
24
+ .join(':')
25
+ if (ms) hms += '+' + padStart(remain, 3, '0')
26
+ return hms
27
+ }
@@ -1,12 +1,12 @@
1
- /**
2
- * Schedules execution on the next immediate cycle and resolves when it runs.
3
- *
4
- * @returns {Promise<void>} Promise resolved on the immediate queue.
5
- */
6
- async function setImmediate () {
7
- return new Promise((resolve) => {
8
- setImmediate(() => resolve())
9
- })
10
- }
11
-
12
- export default setImmediate
1
+ /**
2
+ * Schedules execution on the next immediate cycle and resolves when it runs.
3
+ *
4
+ * @returns {Promise<void>} Promise resolved on the immediate queue.
5
+ */
6
+ async function setImmediate () {
7
+ return new Promise((resolve) => {
8
+ setImmediate(() => resolve())
9
+ })
10
+ }
11
+
12
+ export default setImmediate
@@ -1,56 +1,56 @@
1
- import lockfile from '@bybrave/proper-lockfile2'
2
- import parseDuration from './parse-duration.js'
3
- import fs from 'fs-extra'
4
-
5
- /**
6
- * Like original setInterval, but repeatedly executes a function after a specified delay asynchronously.
7
- *
8
- * Function handler will only be executed if the previous execution has completed. This prevents
9
- * overlapping executions.
10
- *
11
- * Optionally using a lock file. If it is provided and the lock file does not exist, it will be created.
12
- * If the lock file exists and is locked, the handler will not be executed.
13
- *
14
- * @param {Function} handler The function to execute after the interval.
15
- * @param {number|string} timeout The delay in milliseconds or a duration string.
16
- * @param {Object} [options={}] Additional options.
17
- * @param {string} [options.lockFile] The path to the lock file. If provided, the handler will only execute if the lock can be acquired.
18
- * @param {Array} [options.args=[]] Arguments to pass to the handler.
19
- * @param {Object} [options.lockFileOpts] Options for the lock file.
20
- * @param {boolean} [options.silent=false] If true, errors during lock acquisition will be ignored.
21
- * @param {number} [options.maxIterations=Infinity] Maximum number of times to execute the handler.
22
- * @param {Object} [options.scope] The scope to bind the handler to.
23
- * @returns {Promise<void>} Promise resolved after the interval.
24
- */
25
- async function setInterval (handler, timeout, options = {}) {
26
- const { lockFile, args = [], lockFileOpts, silent = false, maxIterations = Infinity, scope } = options
27
- timeout = parseDuration(timeout)
28
- let iterations = 0
29
-
30
- async function execHandler () {
31
- if (!lockFile) {
32
- await handler.call(scope, ...args)
33
- return
34
- }
35
- try {
36
- fs.ensureFileSync(lockFile)
37
- const release = await lockfile.lock(lockFile, lockFileOpts)
38
- await handler.call(scope, ...args)
39
- await release()
40
- } catch (err) {
41
- if (!silent) throw err
42
- console.log(err)
43
- }
44
- }
45
-
46
- async function exec () {
47
- if (iterations >= maxIterations) return
48
- await execHandler()
49
- iterations++
50
- setTimeout(exec, timeout)
51
- }
52
-
53
- exec()
54
- }
55
-
56
- export default setInterval
1
+ import lockfile from '@bybrave/proper-lockfile2'
2
+ import parseDuration from './parse-duration.js'
3
+ import fs from 'fs-extra'
4
+
5
+ /**
6
+ * Like original setInterval, but repeatedly executes a function after a specified delay asynchronously.
7
+ *
8
+ * Function handler will only be executed if the previous execution has completed. This prevents
9
+ * overlapping executions.
10
+ *
11
+ * Optionally using a lock file. If it is provided and the lock file does not exist, it will be created.
12
+ * If the lock file exists and is locked, the handler will not be executed.
13
+ *
14
+ * @param {Function} handler The function to execute after the interval.
15
+ * @param {number|string} timeout The delay in milliseconds or a duration string.
16
+ * @param {Object} [options={}] Additional options.
17
+ * @param {string} [options.lockFile] The path to the lock file. If provided, the handler will only execute if the lock can be acquired.
18
+ * @param {Array} [options.args=[]] Arguments to pass to the handler.
19
+ * @param {Object} [options.lockFileOpts] Options for the lock file.
20
+ * @param {boolean} [options.silent=false] If true, errors during lock acquisition will be ignored.
21
+ * @param {number} [options.maxIterations=Infinity] Maximum number of times to execute the handler.
22
+ * @param {Object} [options.scope] The scope to bind the handler to.
23
+ * @returns {Promise<void>} Promise resolved after the interval.
24
+ */
25
+ async function setInterval (handler, timeout, options = {}) {
26
+ const { lockFile, args = [], lockFileOpts, silent = false, maxIterations = Infinity, scope } = options
27
+ timeout = parseDuration(timeout)
28
+ let iterations = 0
29
+
30
+ async function execHandler () {
31
+ if (!lockFile) {
32
+ await handler.call(scope, ...args)
33
+ return
34
+ }
35
+ try {
36
+ fs.ensureFileSync(lockFile)
37
+ const release = await lockfile.lock(lockFile, lockFileOpts)
38
+ await handler.call(scope, ...args)
39
+ await release()
40
+ } catch (err) {
41
+ if (!silent) throw err
42
+ console.log(err)
43
+ }
44
+ }
45
+
46
+ async function exec () {
47
+ if (iterations >= maxIterations) return
48
+ await execHandler()
49
+ iterations++
50
+ setTimeout(exec, timeout)
51
+ }
52
+
53
+ exec()
54
+ }
55
+
56
+ export default setInterval