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

xp-types

v1.1.5

Published

A pack to handle javascript data types

Downloads

11

Readme

xp-types

A pack to handle javascript data types

Install

npm i xp-types --save

Usage

import { string } from 'xp-types'
// let string = require('xp-types').string

let str = 'I am 韩小平'
console.log(str.length) // 8

let len = string.len(str)
console.log(len) // 11

API

object

object.deepCopy(obj)

  • deep copy object
let obj = { a: 1 }
let obj2 = object.deepCopy(obj)
obj2.a = 2
console.log(obj) // { a: 1 }
console.log(obj2) // { a: 2 }

object.objectify(obj, keys)

  • if obj[key] is undefined, null, 0, etc., change it to {}
let obj = {}
console.log(obj.a) // undefined
object.objectify(obj, ['a'])
console.log(obj.a) // {}

array

array.fastArr(num, from)

  • fast get an array of ordered numbers
let arr1 = array.fastArr(5) // [1, 2, 3, 4, 5]
let arr2 = array.fastArr(5, 6) // [6, 7, 8, 9, 10]

array.unique(arr)

  • remove repeated values, only work for primitive types
let arr = [1, 2, 3, 2]
let arr2 = array.unique(arr) // [1, 2, 3]

array.order(arr, reverse)

  • order array elements, will effect the array, no return value
let arr = [1, 4, 3, 2]
let arr2 = [2, 1, 3, 4]
array.order(arr)
console.log(arr) // [1, 2, 3, 4]

array.order(arr2, true)
console.log(arr2) // [4, 3, 2, 1]

array.arraify(arr)

  • change undefined, null, 0, etc. to []
let arr = null
let ret = array.arraify(arr)
console.log(ret) // []

string

string.len(str)

  • get the real length of a string(1 Chinese charactor equals to 2 English charactors).
let str = 'I am 韩小平'
let len = string.len(str)
console.log(len) // 11

string.ellipsis(str, maxLen, suffix)

  • cut the string if it is more than certain length, and use suffix string to replace the rest.
let str = 'this is a note'
let newStr = string.ellipsis(str, 7)
console.log(newStr) // this is...

string.stringify(str)

  • change undefined, null, 0, etc. to ''
let str
let ret = string.stringify(str)
console.log(ret) // ''

number

number.numberify(num)

let num
let ret = number.numberify(num)
console.log(ret) // 0

url

url.query(name)

  • get url query value, if name isn't set, will return the query object
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.query() // { name: 'xp', test: 1 }
url.query('name') // xp

url.hash(name)

  • get url hash value, if name isn't set, will return the hash object
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.hash() // { token: 'abc', filter: 2 }
url.hash('filter') // 2

url.hostname()

  • get the hostname
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.hostname() // www.example.com

url.domain()

  • get the domain
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.domain() // example.com

url.sub()

  • get the sub
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.sub() // www

date

date.format(time, fmt)

  • get formatted time
let time = new Date()
date.format(time, 'YYYY-MM-DD') // 2017-05-31

date.duration

  • convert seconds to Chinese string
let time = 615
date.duration(time) // 10分15秒

date.ago

  • convert passed time to Chinese string
let time = 3815
date.ago(time) // 1小时前