waibu 2.4.1 → 2.6.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/index.js +59 -1
- package/package.json +1 -1
- package/wiki/CHANGES.md +8 -0
package/index.js
CHANGED
|
@@ -301,7 +301,7 @@ async function factory (pkgName) {
|
|
|
301
301
|
}
|
|
302
302
|
|
|
303
303
|
/**
|
|
304
|
-
* Get origin
|
|
304
|
+
* Get origin from fastify's request object
|
|
305
305
|
*
|
|
306
306
|
* @method
|
|
307
307
|
* @param {Object} req
|
|
@@ -314,6 +314,16 @@ async function factory (pkgName) {
|
|
|
314
314
|
return `${req.protocol}://${host}`
|
|
315
315
|
}
|
|
316
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Get hostname from fastify's request object
|
|
319
|
+
*
|
|
320
|
+
* @param {Object} req
|
|
321
|
+
* @returns {string}
|
|
322
|
+
*/
|
|
323
|
+
getHostname = (req) => {
|
|
324
|
+
return req.hostname.split(':')[0]
|
|
325
|
+
}
|
|
326
|
+
|
|
317
327
|
/**
|
|
318
328
|
* Get plugin by prefix
|
|
319
329
|
*
|
|
@@ -558,6 +568,54 @@ async function factory (pkgName) {
|
|
|
558
568
|
})
|
|
559
569
|
return text
|
|
560
570
|
}
|
|
571
|
+
|
|
572
|
+
arrayToAttr = (array = [], delimiter = ' ') => {
|
|
573
|
+
const { isPlainObject } = this.app.lib._
|
|
574
|
+
return array.map(item => {
|
|
575
|
+
if (isPlainObject(item)) return this.objectToAttr(item)
|
|
576
|
+
return item
|
|
577
|
+
}).join(delimiter)
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
attrToArray = (text = '', delimiter = ' ') => {
|
|
581
|
+
const { map, trim, without, isArray } = this.app.lib._
|
|
582
|
+
if (text === true) text = ''
|
|
583
|
+
if (isArray(text)) text = text.join(delimiter)
|
|
584
|
+
return without(map(text.split(delimiter), i => trim(i)), '', undefined, null).map(item => {
|
|
585
|
+
return item
|
|
586
|
+
})
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
attrToObject = (text = '', delimiter = ';', kvDelimiter = ':') => {
|
|
590
|
+
const { camelCase, isPlainObject } = this.app.lib._
|
|
591
|
+
const result = {}
|
|
592
|
+
if (isPlainObject(text)) text = this.objectToAttr(text)
|
|
593
|
+
if (typeof text !== 'string') return text
|
|
594
|
+
if (text.slice(1, 3) === '%=') return text
|
|
595
|
+
const array = this.attrToArray(text, delimiter)
|
|
596
|
+
array.forEach(item => {
|
|
597
|
+
const [key, val] = this.attrToArray(item, kvDelimiter)
|
|
598
|
+
result[camelCase(key)] = val
|
|
599
|
+
})
|
|
600
|
+
return result
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
base64JsonDecode = (data = 'e30=') => {
|
|
604
|
+
return JSON.parse(Buffer.from(data, 'base64'))
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
base64JsonEncode = (data) => {
|
|
608
|
+
return Buffer.from(JSON.stringify(data)).toString('base64')
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
objectToAttr = (obj = {}, delimiter = ';', kvDelimiter = ':') => {
|
|
612
|
+
const { forOwn, kebabCase } = this.app.lib._
|
|
613
|
+
const result = []
|
|
614
|
+
forOwn(obj, (v, k) => {
|
|
615
|
+
result.push(`${kebabCase(k)}${kvDelimiter}${v ?? ''}`)
|
|
616
|
+
})
|
|
617
|
+
return result.join(delimiter)
|
|
618
|
+
}
|
|
561
619
|
}
|
|
562
620
|
|
|
563
621
|
return Waibu
|
package/package.json
CHANGED