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

js-array-collections

v1.4.7

Published

Prototype methods to extend functionality for javascript arrays and strings, mainly focused on array filtering and sorting

Downloads

10

Readme

###JS Collections Package

Updates "npm install [email protected]" import { Collect, Stringify, Numberfy } from 'js-array-collections'

Collect Extends Array Prototype Motiviation: Laravel Collections

let people = [{ id: 0, name: 'julie' }, { id: 1, name: 'sarah' }, { id: 2, name: 'tim' }];

let collection = Collect(people);

collection.pluck('id') // [0, 1, 2]
collection.where('id', 0) // [{ id: 0, name: 'julie' }]
collection.where('id', '>=', 1) // [{ id: 1, name: 'sarah' }, { id: 2, name: 'tim' }]
collection.firstWhere('name', '=', 'tim') // { id: 2, name: 'tim' }

collection // returns [{ id: 0, name: 'julie' }, { id: 1, name: 'sarah' }, { id: 2, name: 'tim' }]

You can also fluently chain collection methods together

collection.where('id', '=>', 1).where('name', '!=', 'tim').pluck('name') // return ['sarah'] 

Numberfy Extends Number Prototype

let totalItems = 5

let total = Numberfy(totalItems);

total.isBetween(3, 6) // returns true
total.is(3) // returns false
total.isLastIn([1,2,3]) // returns true
total.isFirstIn([1,2,3]) // returns false
total.matchesAny([2, 5, 11, 23, 47, 95]) // returns true
total.matchesAll([2, 5, 11, 23, 47, 95]) // returns false

total // returns 5

Stringify Extends String Prototype

let url = Stringify('https://google.com')
let badUrl = Stringify('htt://@screweyurl@net')

url.isUrl() // returns true
badUrl.isUrl() // returns false
url.isEmail() // returns false
url.isPhoneNumber() // returns false
url.trimLeft() // trim white space from left side
url.truncate(limit = 8, dots = true) // returns htt://@scr...
url // returns 'https://google.com'

Examples:

let posts = Collect([
   {name: 'Good Morning Utah', id: 1, length: 9453, readers: 20000, status: 'published'},
   {name: 'Super Bowl Sunday', id: 2, length: 945, readers: 100, status: 'published'},
   {name: 'Machine Learning', id: 3, length: 119453, readers: 420000, status: 'internationally_published'},
   {name: 'Friday Night Lights', id: 4, length: 3343, readers: 1334, status: 'pending'}, 
]);


posts.where('readers', '<=', 4500).pluck('status')
//returns ['published', 'pending']

posts.take(3).pluck('name').reverse().listify()
// returns 'Machine Learning, Super Bowl Sunday, Good Morning Utah'

posts.take(3).pluck('name').reverse().listify().truncate(35)
// returns 'Machine Learning, Super Bowl Sunday...'

Install

  1. "npm install [email protected]"
  2. import { Collect, Stringify, Numberfy } from 'js-array-collections'

Array/Collection Methods:


/**
 *
 * @param options
 * @returns {Array}
 */
 where(property, condition, value)

 //short hand to check (property === value)
 where(property, value) 
 

 whereHas(property, callback)
 
 //returns students where student.gpa is gpa >= 3.5
 students.whereHas('gpa', (gpa) => (gpa >= 3.5))


whereHasProperty(property, propertyIncludes)

validation = [
  {
  	 field: 'name',
  	 message: 'Name Field Is Required',
  	 rules: ['required', 'max:255']
  },
  {
  	field: 'email',
  	message: 'Email Field is Required',
  	rules: ['email']
  }
]
 

//
// Array of objects with array property where array property includes value
// ========================================================================
// Each Object has ARRAY property 
// We only want the objects where the ARRAY property includes given value
//
// Real Life Use Case
//=====================================
// Validation Of Form Fields

// validation = Collect([ 
//    { field: 'name', rules: ['max:255'] }, 
//    { field: 'number', rules: ['required'] }, 
//    { field: 'email', rules: ['email', 'required'] }
// ]);

// Goal: return Validation Fields where field.rules includes "email"
// ========================
//
//
// Normal Logic To Achieve GoalImplement
// ==========================================
// validation items WHERE
// item HAS rules 
// AND validation[itemIndex].rules
// INCLUDES required
//
// OR 
//
// use our whereHasProperty method

validation.whereHasProperty('rules', 'required')

/**
 *
 * @param property
 * @param context
 * @returns {*}
 */
orderBy(property, context = 'asc')


/**
 *
 * @returns {*[]}
 */
flatten()


/**
 *
 * @param property
 * @returns {*}
 */
flatPluck(property)


/**
 *
 * @param property
 * @returns {Array}
 */
groupBy(property)


/**
 *
 * @returns {string}
 */
stringify()


/**
 *
 * @returns {string}
 */
listify()


/**
 *
 * @returns {*}
 */
random()


/**
 *
 * @param $property
 * @param $condition
 * @param $expectation
 * @returns {*}
 */
where(property, condition, expectation)


/**
 *
 * @param callback
 * @returns {Array|Object}
 */
each(callback)


/**
 *
 * @returns {Array}
 */
tail()


/**
 *
 * @param number
 * @returns {*[]}
 */
take(numberOfElements)


/**
 *
 * @returns {*}
 */
first()


/**
 *
 * @returns {*}
 */
last()


/**
 *
 * @param options
 * @returns {Array}
 */
pluck(prop)


/**
 *
 * @returns {Array}
 */
unique()


/**
 *
 * @param args
 * @returns {Array}
 */
append(elements)


/**
 *
 * @param number
 * @returns {*}
 */
count()


/**
 *
 * @param by
 * @returns {Array}
 */
chunk(byIncrement)


/**
 *
 * @param $property
 * @param $condition
 * @param $expected
 * @returns {*}
 */
firstWhere(property, condition, expectation)


/**
 *
 * @param $property
 * @param $condition
 * @param $expected
 * @returns {*}
 */
lastWhere(property, condition, expectation)


/**
 *
 * @returns {*|number}
 */
sum()


/**
 *
 * @param array
 * @returns {*}
 */
intersect(array)


/**
 *
 * @returns {boolean}
 */
empty()


/**
 *
 * @returns {number}
 */
lastIndex()


/**
 *
 * @param args
 * @returns {Array}
 */
prepend(array)

More Examples:



//Example one - where
`let smallPosts = posts.where('readers', '<=', 100);`


`[{name: 'asdfasdfsdf', id: 2, length: 945, readers: 100, status:'published'}]`



//Example two - pluck property
`let names = post.pluck('name');`

`['Sunday Morning Post', 'asdfasdfsdf', 'dasdf', 'Friday Night Lights'];`


//Example three - chaining several find total reader from published posts

`let totalReadersFromActivePosts = posts.where('status', '=', 'published').pluck('readers').sum()`

`numberOfReadersAddedUpFromAllPublishedPosts`



//Example four - find the average number of readers where posts are not published

`let avgReadersForNonPublishedPosts = posts.where('status', '!=', 'published').pluck('readers').avg()`

`avg Number of readers for Non Published Posts`