npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nblue-extend

v1.0.12

Published

nblue modules to extend native object

Downloads

4,024

Readme

nblue-extend

nblue module to extend javascript native objects

![Gitter](https://badges.gitter.im/Join Chat.svg)

NPM

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.