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

sp-data

v1.2.0

Published

Common Data Trans methods for Date, Array, Object, Array of Objects etc.

Downloads

6

Readme

sp-data

Common Data Trans methods for Date, Array, Object, Array of Objects etc.


Install

NPM

npm install --save sp-data
// or yarn add sp-data

CDN

<script src="https://unpkg.com/sp-data/index.min.js"></script>
<script>
  __DATA.array.create(5, 0);
  //=> [0, 0, 0, 0, 0]
</script>

Usage

 //sp: {date, array, object, parse, entity, string}
const sp = require('sp-data');

sp.date.format('yyyy-MM-dd', new Date());
//=> 2018-08-06

sp.array.sort(arr = [1, 2, 3, 4, 1], isASC = true)
//=>[1, 1, 2, 3, 4]

sp.array.sort(arr = [1, 2, 3, 4, 1], isASC = false)
//=>[4, 3, 2, 1, 1]

Date

//date: format(), goto(), prevDay(), prevMonth(), prevYear(), nextDay(), nextMonth(), nextYear(), calc()
const date = require('sp-data/date');

date.format('yyyy-MM-dd', new Date());
//=> 2018-08-06

date.goto(year, month, day);
//=> return Date

date.prevDay(dayCount = 0, startDate = new Date());
//=> return Date

date.prevMonth(monthCount = 0, date = new Date());
//=> return Date

date.prevYear(yearCount = 0, date = new Date());
//=> return Date

date.nextDay(dayCount = 0, startDate = new Date());
//=> return Date

date.nextMonth(monthCount = 0, date = new Date());
//=> return Date

date.nextYear(yearCount = 0, date = new Date());
//=> return Date

date.calc(startDate = new Date(2018, 01, 01), endDate = new Date());
//=> { seconds: 20, minutes: 44, hours: 15, days: 188 }

date.calc(startDate = new Date(2018, 01, 01), endDate = new Date(2000, 01, 01));
//=> { seconds: 0, minutes: 0, hours: 0, days: 6575 }

Array && Array of Objects

 //arr: create(), sort(), sortObj(), unique(), uniqueObj(), filter(), sum(), createByArrObj(), mergeObj()
const arr = require('sp-data/array');

arr.create((length = 5, fillVal = 'a'))
//=>['a', 'a', 'a', 'a', 'a']

arr.sort(arr = [1, 2, 3, 4, 1], isASC = true)
//=>[1, 1, 2, 3, 4]

arr.sort(arr = [1, 2, 3, 4, 1], isASC = false)
//=>[4, 3, 2, 1, 1]

arr.sortObj(arr = [{name: 'Alice', age: 12},{name: 'Bob', age: 14}, {name: 'John', age: 11}], keyName = 'age', isASC = true)
//=>[{name: 'John', age: 11}, {name: 'Alice', age: 12},{name: 'Bob', age: 14}]

arr.sortObj(arr = [{name: 'Alice', age: 12},{name: 'Bob', age: 14}, {name: 'John', age: 11}], keyName = 'age', isASC = false)
//=>[{name: 'Bob', age: 14}, {name: 'Alice', age: 12}, {name: 'John', age: 11}]

arr.unique(arr = [1, 2, 3, 4, 1])
//=>[1, 2, 3, 4]

//unique by all key
arr.uniqueObj(arr = [{a: 1, b: 2}, {a: 2, b: 3}, {a: 1}, {a: 1}])
//=>[{a: 1, b: 2}, {a: 2, b: 3}, {a: 1}]

//unique by key name
arr.uniqueObj(arr = [{a: 1, b: 2}, {a: 2, b: 3}, {a: 1}], keyName = 'a')
//=>[{a: 1, b: 2}, {a: 2, b: 3}]

arr.filter(arr = [1, 3, 4, 5], function(item) {return item > 3;})
//=>[4, 5]

arr.filter(arr = [{name: 'Alice', age: 12},{name: 'Bob', age: 14}, {name: 'John', age: 11}], function(item) {return item.age > 12;})
//=>[{name: 'Bob', age: 14}]

arr.sum(arr = [1, 2, 3])
//=>6

//item is NOT number string return 0
arr.sum(arr = [1, 2, 3, 'a', 'b'])
//=>6

//sum by index => value
arr.sum(arr = [[1, 2, 3], [4, 5, 6], [7, 8], [9]], idDeep = true)
//[1 + 4 + 7 + 9, 2 + 5 + 8, 9]
//=>[ 21, 15, 9]

//sum by key => value
arr.sum(arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5}, {c: 6}, {d: 7}], idDeep = true)
//[{a: 1 + 4, b: 2 + 5, c: 3 + 6, d: 7}]
//=>[ { a: 5, b: 7, c: 9, d: 7 } ]

arr.createByArrObj(arrObj = [ { a: 3, b: 7}, {a: 0, b: 3}, {a: 2, b: 4} ], indexName = 'a', valueName = 'b')
//=>[ 3, 0, 4 ]

arr.createByArrObj(arrObj = [ { a: 3, b: 7},{a: 0, b: 3}, {a: 2, b: 4} ], indexName = 'a', valueName = 'b', length = 6)
//=>[ 3, 0, 4, 7, 0, 0 ]

arr.createByArrObj(arrObj = [ { a: 3, b: 7}, {a: 0, b: 3}, {a: 2, b: 4} ], indexName = 'a', valueName = 'b', length = 6, defaultValue = 666)
//=>[ 3, 666, 4, 7, 666, 666 ]

let src = [{city:'city1',total:0},{city:'city2',total:0}]
let dest = [{city:'city1',total:10},{city:'city2',total:0}]
arr.mergeObj(src,mergeName = 'city', dest)
//=> [{city:'city1',total:10},{city:'city2',total:0}]

Object

//obj: reverse(), rename()
const obj = require('sp-data/object');

obj.reverse(obj = {a: 1, b: 2})
//=> {1: a, 2: b}

obj.reverse(obj = {a: 1, b: 2}, keyName = 'a')
//=> {1: a, b: 2}

obj.reverse(obj = [{a: 1, b: 2}, {a: 2, b: 3, d: 5}])
//=> [ { '1': 'a', '2': 'b' }, { '2': 'a', '3': 'b', '5': 'd' } ]

//last value will be the last key
obj.reverse(obj = [{a: 1, b: 2}, {a: 2, b: 3, c: 5, d: 5, e: 5}])
//=>[ { '1': 'a', '2': 'b' }, { '2': 'a', '3': 'b', '5': 'e' } ]

obj.reverse(obj = [{a: 1, b: 2}, {a: 2, b: 3, d: 5}], keyName = 'a')
//=> [ { '1': 'a', b: 2 }, { '2': 'a', b: 3, d: 5 } ]

//Object {}
obj.rename(obj = { a: 2, b: 3, c: 5, d: 5, e: 5 })
//=>{ a: 2, b: 3, c: 5, d: 5, e: 5 }

obj.rename(obj = { a: 2, b: 3, c: 5, d: 5, e: 5 }, target = 'a')
//=>{b: 3, c: 5, d: 5, e: 5 }

obj.rename(obj = { a: 2, b: 3, c: 5, d: 5, e: 5 }, target = 'a', replacer = 'x')
//=>{x:2, b: 3, c: 5, d: 5, e: 5 }

//Array of Objects
obj.rename(obj = [{ a: 2, b: 3}, {a: 3, b: 5}])
//=>[{ a: 2, b: 3}, {a: 3, b: 5}]

obj.rename(obj = [{ a: 2, b: 3}, {a: 3, b: 5}], 'a')
//=>[{b: 3}, {b: 5}

obj.rename(obj = [{ a: 2, b: 3, c: 4}, {a: 3, b: 5}], 'c', 'x')
//=>[{ a: 2, b: 3, x: 4}, {a: 3, b: 5}]

Parse

//parse: number(), bollean()
const parse = require('sp-data/parse');

//undefined, [] ,{} ,NaN ,'' ,' ', null or not number string return 0
parse.number(num = [], fixedLength = 0)
//=> 0

parse.number(num = {}, fixedLength = 2)
//=> 0.00

parse.number(num = 'a', fixedLength = 2)
//=> 0.00

parse.number(num = '2', fixedLength = 2)
//=> 2.00

parse.number(num = '2.000', fixedLength = 2)
//=> 2.00

//undefined, [] ,{} ,NaN ,'' ,' ', null return false
parse.boolean(0)
//=> false

//undefined, [] ,{} ,NaN ,'' ,' ', null return false
parse.boolean('a')
//=> true

entity

//entity: encode(str), decode(str)
const entity = require('sp-data/entity');
entity.encode('1')
// &#49;
entity.decode('&#49;&#50;&#51;')
// 123

string

//entity: encode(str), decode(str)
const string = require('sp-data/string')
string.replace('你我他',{你:'ni',我:'wo'})
// niwo他

License

MIT