nblue-extend
v1.0.12
Published
nblue modules to extend native object
Downloads
4,024
Maintainers
Readme
nblue-extend
nblue module to extend javascript native objects
![Gitter](https://badges.gitter.im/Join Chat.svg)
Install:
npm install nblue-extend
Testing:
npm install mocha --save-dev
npm test
Usage:
Class IIf
Returns one of two objects or function, depending on the evaluation of an expression.
// IIf example
const lib = require('nblue-extend')
const IIf = lib.IIf
// return an object
const rt = IIf(true, 1, 0)
// output 1
console.log(rt)
// return a function
const rt = IIf(() => true, () => 1, () => 0)
// output 1
console.log(rt)
// return a Promise
const rt = IIf(true, () => Promise.resolve(), () => Promise.reject(-1)
// output 1
rt.then((data) => console.log(data))
Usage:
Class StringBuilder
A class of StringBuilder for javascript
// StringBuilder example
const lib = require('nblue-extend')
const StringBuilder = lib.StringBuilder
const sb = new StringBuilder()
// append string or remove chars
sb.append('abc')
sb.append('def')
sb.remove(2, 2)
// output 'abef'
console.log(sb.toString())
// output 4
console.log(sb.length)
/* other methods */
// append string with new line ("abc\r\n")
sb.appendLine('abc') //
// append formatted string ("abcaaadef")
sb.appendFormat('abc%sdef', 'aaa')
// it likes use template as below
const str = 'aaa'
sb.append(`abc${str}def`)
// insert string
sb.insert(2, 'fff')
// insert formatted string
sb.insertFormat(0, 'abc%sdef', 'aaa')
// clear all buffer
sb.clear()
Usage:
Class UUID
To create UUID with javascript
// StringBuilder example
const lib = require('nblue-extend')
const UUID = lib.UUID
// output 'd446be70-dc96-11e6-8309-0d01090e080f'
console.log(UUID.generate('v1'))
// output '03010607-0508-4d03-8f06-0b080a0a0904'
console.log(UUID.generate('v4'))
// output '{0d080704-0b08-4709-8102-0a010f00080e}'
console.log(UUID.generate('guid'))
extends Date, support format data to special string value
Parse String value to Date
// parse a string value to Date type
// use static method of parseDate
const dt1 = Date.parseDate('2016/02/05 14:33:12')
// or call toDate() method for string value
const dt2 = '2016/02/05 14:33:12'.toDate()
// it equal to
new Date(Date.parse('2016/02/05 14:33:12'))
Output formatted date value
// output "2016"
console.log(dt1.format('yyyy'))
// output "02/05/2016"
console.log(dt1.format('MM/dd/yyyy'))
// output "14:33:12"
console.log(dt1.format('HH:mm:ss'))
Date and time patterns
- yy = short year
- yyyy = long year
- M = month (1-12)
- MM = month (01-12)
- MMM = month abbreviation (Jan, Feb ... Dec)
- MMMM = long month (January, February ... December)
- d = day (1 - 31)
- dd = day (01 - 31)
- ddd = day of the week in words (Monday, Tuesday ... Sunday)
- dddd = short day of the week in words (Mon, Tue ... Sun)
- h = hour in am/pm (0-12)
- hh = hour in am/pm (00-12)
- H = hour in day (0-23)
- HH = hour in day (00-23)
- mm = minute (00-59)
- m = minute (0-59)
- ss = second (00-59)
- s = second (0-59)
- S = milliseconds
- TT = AM/PM marker
- tt = a.m./p.m. marker
extends Promise
Extends methods of Promise (finally(), done(), filter() and map()
)
// method of finally
Promise.
resolve(0).
then(() => Promise.reject(-1)).
catch((err) => {
console.log('#err')
}).
finally(() => {
console.log('#finally')
})
// method of done
// it will throw uncaught error
Promise.
resolve(0).
then(() => Promise.reject(-1)).
done()
// filter and map value for resolve result by Promise
// like array.prototype.filter and array.prototype.map
// method of filter
Promise.
resolve([1, 2, 3, 4]).
filter((val) => val > 2).
then((data) => {
// output [3, 4]
console.log(data)
})
// method of map
Promise.
resolve([1, 2, 3, 4]).
map((val) => val * 2).
then((data) => {
// output [1, 4, 6, 8]
console.log(data)
})
support Map and Object convert each other.
For example, Map to Object
const map = new Map()
map.
set('key1', 'val1').
set('key2', 'val2').
set('key3', 'val3')
const obj = map.toObject()
// output converted object
console.log(obj)
For example, Object to Map
const obj = {
key1: 'val1',
key2: 'val2',
key3: 'val3'
}
const map = obj.toMap()
// output converted map
console.log(map)
License
The MIT License (MIT)
Copyright © 2017 Bruce Jiang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.