aneka 0.1.0 → 0.1.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/index.js CHANGED
@@ -0,0 +1,31 @@
1
+ import arrangeArray from './src/arrange-array.js'
2
+ import defaultsDeep from './src/defaults-deep.js'
3
+ import delay from './src/delay.js'
4
+ import extractText from './src/extract-text.js'
5
+ import getKeyByValue from './get-key-by-value.js'
6
+ import includes from './src/includes.js'
7
+ import isClass from './src/is-class.js'
8
+ import isSet from './src/is-set.js'
9
+ import log from './src/log.js'
10
+ import paginate from './src/paginate.js'
11
+ import pascalCase from './src/pascal-case.js'
12
+ import round from './src/round.js'
13
+ import secToHms from './src/sec-to-hms.js'
14
+ import titleize from './titleize.js'
15
+
16
+ export default {
17
+ arrangeArray,
18
+ defaultsDeep,
19
+ delay,
20
+ extractText,
21
+ getKeyByValue,
22
+ includes,
23
+ isClass,
24
+ isSet,
25
+ log,
26
+ paginate,
27
+ pascalCase,
28
+ round,
29
+ secToHms,
30
+ titleize
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aneka",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "cemacemla",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -0,0 +1,18 @@
1
+ import trim from 'lodash-es/trim.js'
2
+
3
+ function arrangeArray (inputs = [], trimItem = true) {
4
+ const first = []
5
+ const last = []
6
+
7
+ const items = inputs.filter(item => {
8
+ if (trimItem) item = trim(item)
9
+ if (item[0] === '^') first.push(item.slice(1))
10
+ else if (item[0] === '$') last.push(item.slice(1))
11
+ return !['^', '$'].includes(item[0])
12
+ })
13
+ items.unshift(...first)
14
+ items.push(...last)
15
+ return items
16
+ }
17
+
18
+ export default arrangeArray
@@ -0,0 +1,14 @@
1
+ import mergeWith from 'lodash-es/mergeWith.js'
2
+ import isArray from 'lodash-es/isArray.js'
3
+
4
+ function defaultsDeep (...args) {
5
+ const output = {}
6
+ args.reverse().forEach(function (item) {
7
+ mergeWith(output, item, function (objectValue, sourceValue) {
8
+ return isArray(sourceValue) ? sourceValue : undefined
9
+ })
10
+ })
11
+ return output
12
+ }
13
+
14
+ export default defaultsDeep
@@ -0,0 +1,15 @@
1
+ function extractText (text, patternStart, patternEnd) {
2
+ let result = ''
3
+ const open = text.indexOf(patternStart)
4
+ if (open > -1) {
5
+ text = text.slice(open + patternStart.length)
6
+ const close = text.indexOf(patternEnd)
7
+ if (close > -1) {
8
+ result = text.slice(0, close)
9
+ }
10
+ }
11
+ const pattern = `${patternStart}${result}${patternEnd}`
12
+ return { result, pattern }
13
+ }
14
+
15
+ export default extractText
@@ -0,0 +1,5 @@
1
+ function getKeyByValue (object, value) {
2
+ return Object.keys(object).find(key => object[key] === value)
3
+ }
4
+
5
+ export default getKeyByValue
@@ -0,0 +1,11 @@
1
+ function includes (matcher = [], array = []) {
2
+ if (typeof matcher === 'string') matcher = [matcher]
3
+ let found = false
4
+ for (const m of matcher) {
5
+ found = array.includes(m)
6
+ if (found) break
7
+ }
8
+ return found
9
+ }
10
+
11
+ export default includes
@@ -0,0 +1,7 @@
1
+ function isClass (item) {
2
+ return typeof item === 'function' &&
3
+ Object.prototype.hasOwnProperty.call(item, 'prototype') &&
4
+ !Object.prototype.hasOwnProperty.call(item, 'arguments')
5
+ }
6
+
7
+ export default isClass
@@ -0,0 +1,29 @@
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
+ function paginate (collection = [], { page = 1, limit = 25, sort } = {}) {
7
+ const count = collection.length
8
+ const offset = (page - 1) * limit
9
+ const fields = []
10
+ const dirs = []
11
+ if (isPlainObject(sort)) {
12
+ forOwn(sort, (v, k) => {
13
+ fields.push(k)
14
+ dirs.push(v < 0 ? 'desc' : 'asc')
15
+ })
16
+ }
17
+ if (!isEmpty(fields)) collection = orderBy(collection, fields, dirs)
18
+ const data = collection.slice(offset, offset + limit)
19
+
20
+ return {
21
+ data,
22
+ page,
23
+ limit,
24
+ count,
25
+ pages: Math.ceil(collection.length / limit)
26
+ }
27
+ }
28
+
29
+ export default paginate
@@ -0,0 +1,8 @@
1
+ import upperFirst from 'lodash-es/upperFirst.js'
2
+ import camelCase from 'lodash-es/camelCase.js'
3
+
4
+ function pascalCase (text) {
5
+ return upperFirst(camelCase(text))
6
+ }
7
+
8
+ export default pascalCase
package/src/round.js ADDED
@@ -0,0 +1,6 @@
1
+ function round (val, scale = 0) {
2
+ scale = scale <= 0 ? 1 : 10 ** scale
3
+ return Math.round(val * scale) / scale
4
+ }
5
+
6
+ export default round
@@ -0,0 +1,26 @@
1
+ import forOwn from 'lodash-es/forOwn.js'
2
+ import map from 'lodash-es/map.js'
3
+ import words from 'lodash-es/words.js'
4
+ import uniq from 'lodash-es/uniq.js'
5
+ import concat from 'lodash-es/concat.js'
6
+ import upperFirst from 'lodash-es/upperFirst.js'
7
+
8
+ function titleize (text = '', { ignores = [], replacement = {} } = {}) {
9
+ const defIgnores = ['or', 'and', 'of', 'with']
10
+ const replacer = {}
11
+ forOwn(replacement, (v, k) => {
12
+ const id = this.generateId('int')
13
+ replacer[id] = k
14
+ text = text.replace(k, ` ${id} `)
15
+ })
16
+ return map(words(text), t => {
17
+ forOwn(replacer, (v, k) => {
18
+ if (k === t) t = replacement[replacer[k]]
19
+ })
20
+ ignores = uniq(concat(ignores, defIgnores))
21
+ if (ignores.includes(t)) return t
22
+ return upperFirst(t)
23
+ }).join(' ')
24
+ }
25
+
26
+ export default titleize