aneka 0.0.3 → 0.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aneka",
3
- "version": "0.0.3",
3
+ "version": "0.0.7",
4
4
  "description": "cemacemla",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,6 +21,9 @@
21
21
  },
22
22
  "homepage": "https://github.com/ardhi/aneka#readme",
23
23
  "dependencies": {
24
- "lodash": "^4.17.21"
24
+ "axios": "^0.24.0",
25
+ "lodash": "^4.17.21",
26
+ "log-timestamp": "^0.3.0",
27
+ "query-string": "^7.0.1"
25
28
  }
26
29
  }
@@ -0,0 +1,11 @@
1
+ const { each, concat, reject, find } = require('lodash')
2
+
3
+ module.exports = function (coll = [], col = '', names = []) {
4
+ const picked = []
5
+ const misc = reject(coll, c => names.includes(c[col]))
6
+ each(names, n => {
7
+ const item = find(coll, c => c[col] === n)
8
+ if (item) picked.push(item)
9
+ })
10
+ return concat(picked, misc)
11
+ }
@@ -0,0 +1,7 @@
1
+ // source: https://flaviocopes.com/how-to-divide-collay-js/
2
+
3
+ module.exports = function (coll, itemCount) {
4
+ return new Array(Math.ceil(coll.length / itemCount))
5
+ .fill()
6
+ .map(_ => coll.splice(0, itemCount))
7
+ }
@@ -0,0 +1,16 @@
1
+ // source: https://flaviocopes.com/how-to-divide-collay-js/
2
+ module.exports = function (coll, groupCount) {
3
+ const result = []
4
+ for (let i = 0; i < groupCount; i++) {
5
+ result.push([])
6
+ }
7
+ const wordsPerLine = Math.ceil(coll.length / groupCount)
8
+ for (let line = 0; line < groupCount; line++) {
9
+ for (let i = 0; i < wordsPerLine; i++) {
10
+ const value = coll[i + line * wordsPerLine]
11
+ if (!value) continue
12
+ result[line].push(value)
13
+ }
14
+ }
15
+ return result
16
+ }
@@ -0,0 +1,13 @@
1
+ module.exports = function (coll, page = 1, rowsPerPage = 25) {
2
+ const total = coll.length
3
+ const offset = (page - 1) * rowsPerPage
4
+ const rows = coll.slice(offset, offset + rowsPerPage)
5
+
6
+ return {
7
+ rows,
8
+ page,
9
+ rowsPerPage,
10
+ total,
11
+ totalPages: Math.ceil(coll.length / rowsPerPage)
12
+ }
13
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = function (ms = 1000) {
2
+ return new Promise((resolve, reject) => {
3
+ setTimeout(() => {
4
+ resolve()
5
+ }, ms)
6
+ })
7
+ }
@@ -0,0 +1,5 @@
1
+ const print = require('./print')
2
+
3
+ module.exports = msg => {
4
+ print(msg, true, 'error')
5
+ }
@@ -0,0 +1,52 @@
1
+ const axios = require('axios')
2
+ const { has, isString, omit } = require('lodash')
3
+ const qs = require('query-string')
4
+
5
+ const handleSuccess = (resp, opts, cfg) => {
6
+ return resp.data
7
+ }
8
+
9
+ const handleError = (err, opts, cfg) => {
10
+ return err
11
+ }
12
+
13
+ module.exports = function (opts = {}, cfg = {}) {
14
+ return new Promise((resolve, reject) => {
15
+ if (isString(opts)) opts = { url: opts }
16
+ opts.params = opts.params || {}
17
+ if (!has(cfg, 'cacheBuster')) cfg.cacheBuster = true
18
+ if (cfg.cacheBuster) opts.params[cfg.cacheBusterKey || '_'] = Date.now()
19
+ if (cfg.streamHandler) {
20
+ opts.url = `${opts.url}?${qs.stringify(opts.params)}`
21
+ let error
22
+ fetch(opts.url, omit(opts, ['url', 'params']))
23
+ .then(resp => {
24
+ if (!resp.ok) {
25
+ error = resp
26
+ return false
27
+ }
28
+ return cfg.streamHandler(resp)
29
+ })
30
+ .then(resp => {
31
+ if (!resp) return false
32
+ resolve(handleSuccess({ data: resp }, opts, cfg))
33
+ })
34
+ .then(resp => {
35
+ const e = new Error(error.statusText)
36
+ e.status = error.status
37
+ reject(handleError(e, opts, cfg))
38
+ })
39
+ .catch(err => {
40
+ reject(handleError(err, opts, cfg))
41
+ })
42
+ return
43
+ }
44
+ axios(opts)
45
+ .then(resp => {
46
+ resolve(handleSuccess(resp, opts, cfg))
47
+ })
48
+ .catch(err => {
49
+ reject(handleError(err, opts, cfg))
50
+ })
51
+ })
52
+ }
@@ -0,0 +1,3 @@
1
+ module.exports = function (input) {
2
+ return !(input === null || input === undefined)
3
+ }
@@ -0,0 +1,7 @@
1
+ require('log-timestamp')
2
+
3
+ module.exports = (msg, terminate, method = 'log') => {
4
+ if (method === true) method = 'log'
5
+ console[method](msg)
6
+ if (terminate) process.exit(1)
7
+ }
@@ -0,0 +1,14 @@
1
+ const { forOwn } = require('lodash')
2
+
3
+ module.exports = function(params) {
4
+ const result = {}
5
+ forOwn(params, (v, k) => {
6
+ let val
7
+ if (v === 'true') val = true
8
+ else if (v === 'false') val = false
9
+ else if (Number(v)) val = Number(v)
10
+ else val = v
11
+ result[k] = val
12
+ })
13
+ return result
14
+ }