aneka 0.7.3 → 0.8.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/CHANGES.md CHANGED
@@ -13,6 +13,9 @@
13
13
  - [0.7.1] Bug fix in ```parseObject()```
14
14
  - [0.7.2] Bug fix in ```parseArgsArgv()```
15
15
  - [0.7.3] Rewrite options in ```parseObject()```
16
+ - [0.7.4] Bug fix in ```parseObject()```
17
+ - [0.8.0] Add ```randomRange()```
18
+ - [0.8.0] Add ```generateId()```
16
19
 
17
20
  ## 2025-12-11
18
21
 
package/index.js CHANGED
@@ -25,6 +25,8 @@ import parseArgsArgv from './src/parse-args-argv.js'
25
25
  import parseEnv from './src/parse-env.js'
26
26
  import parseDuration from './src/parse-duration.js'
27
27
  import parseObject from './src/parse-object.js'
28
+ import randomRange from './src/random-range.js'
29
+ import generateId from './src/generate-id.js'
28
30
 
29
31
  export {
30
32
  arrangeArray,
@@ -35,6 +37,7 @@ export {
35
37
  formatText,
36
38
  filterDuplicate,
37
39
  filterIndex,
40
+ generateId,
38
41
  getIndex,
39
42
  getKeyByValue,
40
43
  includes,
@@ -49,6 +52,7 @@ export {
49
52
  parseEnv,
50
53
  parseObject,
51
54
  pascalCase,
55
+ randomRange,
52
56
  resolvePath,
53
57
  round,
54
58
  secToHms,
@@ -66,6 +70,7 @@ export default {
66
70
  formatText,
67
71
  filterDuplicate,
68
72
  filterIndex,
73
+ generateId,
69
74
  getIndex,
70
75
  getKeyByValue,
71
76
  includes,
@@ -80,6 +85,7 @@ export default {
80
85
  parseEnv,
81
86
  parseObject,
82
87
  pascalCase,
88
+ randomRange,
83
89
  resolvePath,
84
90
  round,
85
91
  secToHms,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aneka",
3
- "version": "0.7.3",
3
+ "version": "0.8.0",
4
4
  "description": "Some usefull function helper for daily use",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -28,6 +28,7 @@
28
28
  "flat": "^6.0.1",
29
29
  "lodash-es": "^4.17.21",
30
30
  "ms": "^2.1.3",
31
+ "nanoid": "^5.1.6",
31
32
  "sprintf-js": "^1.1.3",
32
33
  "yargs": "^18.0.0"
33
34
  },
@@ -0,0 +1,35 @@
1
+ import { customAlphabet } from 'nanoid'
2
+
3
+ /**
4
+ * Generate unique random characters that can be used as ID. Use {@link https://github.com/ai/nanoid|nanoid} under the hood
5
+ *
6
+ * @method
7
+ * @memberof module:Lib
8
+ * @param {(boolean|string|Object)} [options={}] - Options. If set to ```true``` or ```alpha```, it will generate alphaphet only characters. If set to ```int```, it will generate integer only characters. Otherwise:
9
+ * @param {string} [options.pattern] - Character pattern to use. Defaults to all available alphanumeric characters
10
+ * @param {number} [options.length=13] - Length of resulted characters
11
+ * @param {string} [options.case] - If set to ```lower``` to use lower cased pattern only. For upper cased pattern, set it to ```upper```
12
+ * @param {boolean} [options.returnInstance] - Set to ```true``` to return {@link https://github.com/ai/nanoid|nanoid} instance instead of string
13
+ * @returns {(string|Object)} Return string or instance of {@link https://github.com/ai/nanoid|nanoid}
14
+ */
15
+ function generateId (options = {}) {
16
+ let type
17
+ if (options === true) options = 'alpha'
18
+ if (options === 'int') {
19
+ type = options
20
+ options = { pattern: '0123456789', length: 15 }
21
+ } else if (options === 'alpha') {
22
+ type = options
23
+ options = { pattern: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', length: 15 }
24
+ }
25
+ let { pattern, length = 13, returnInstance } = options
26
+ pattern = pattern ?? 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
27
+ if (options.case === 'lower') pattern = pattern.toLowerCase()
28
+ else if (options.case === 'upper') pattern = pattern.toUpperCase()
29
+ const nid = customAlphabet(pattern, length)
30
+ if (returnInstance) return nid
31
+ const value = nid()
32
+ return type === 'int' ? parseInt(value) : value
33
+ }
34
+
35
+ export default generateId
@@ -20,6 +20,7 @@ import { set, isString, cloneDeep, isPlainObject, isArray, omit } from 'lodash-e
20
20
  */
21
21
  function parseObject (input, options = {}) {
22
22
  const { silent = true, parseValue = false, translator = {} } = options
23
+ const { prefix, lang, handler } = translator
23
24
  const statics = ['*']
24
25
  if (isString(input)) {
25
26
  try {
@@ -43,12 +44,12 @@ function parseObject (input, options = {}) {
43
44
  if (isArray(obj[k][idx])) obj[k][idx] = obj[k][idx].map(item => typeof item === 'string' ? item.trim() : item)
44
45
  })
45
46
  } else if (isSet(v)) {
46
- if (isString(v) && translator.handler && translator.matcher && translator.matcher(v)) v = translator.handler(v.slice(2))
47
+ if (isString(v) && prefix && lang && handler && v.startsWith(prefix)) v = handler(v.slice(prefix.length))
47
48
  try {
48
49
  if (statics.includes(v)) obj[k] = v
49
- else if (isString(v)) {
50
- const newK = k.slice(2)
51
- if (translator.handler && translator.matcher && translator.matcher(k)) obj[newK] = translator.handler(v)
50
+ else if (isString(v) && prefix && handler && k.startsWith(prefix)) {
51
+ const newK = k.slice(prefix.length)
52
+ if (lang) obj[newK] = handler(v)
52
53
  else obj[newK] = v
53
54
  mutated.push(k)
54
55
  } else if (parseValue) {
@@ -0,0 +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
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
package/src/titleize.js CHANGED
@@ -4,12 +4,13 @@ import words from 'lodash-es/words.js'
4
4
  import uniq from 'lodash-es/uniq.js'
5
5
  import concat from 'lodash-es/concat.js'
6
6
  import upperFirst from 'lodash-es/upperFirst.js'
7
+ import generateId from './generate-id.js'
7
8
 
8
9
  function titleize (text = '', { ignores = [], replacement = {} } = {}) {
9
10
  const defIgnores = ['or', 'and', 'of', 'with']
10
11
  const replacer = {}
11
12
  forOwn(replacement, (v, k) => {
12
- const id = this.generateId('int')
13
+ const id = generateId()
13
14
  replacer[id] = k
14
15
  text = text.replace(k, ` ${id} `)
15
16
  })
@@ -0,0 +1,10 @@
1
+ import { expect } from 'chai'
2
+ import fn from '../src/parse-object.js'
3
+
4
+ describe('parseObjecy', function () {
5
+ it('Should return the expected result', function () {
6
+ const items = { _: { log: { pretty: true, level: 'trace' }, lang: 'id' } }
7
+ const result = fn(items, { parseValue: true })
8
+ expect(result).to.eql(items)
9
+ })
10
+ })