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-split

v1.0.1

Published

It is simple function that gets Object (or Array, or String) and returns parts of this.

Downloads

2

Readme

js-split

What is it?

It is simple function that gets Object (or Array, or String) and returns parts of this.

Installation

npm install --save js-split

How it works?

Sometimes we need split object by keys and we cant do it by JS native functions. js-split can help you with it!

This package includes two functions: main function that exported by default and extra function select.

import jsSplit, { select } from 'js-split';
...

Split Objects

jsSplit(myObject: Object, partKeys1: Array or Object, [partKeys2: Array or Object, ...]): Array

result is Array that contains all parts and rest of myObject ([part1, part2, ..., rest])

  import jsSplit from 'js-split';
  const v1 = jsSplit({a: 1, b: 2, c: 3, d: 4}, ['a'], ['c']); 
  // v1 = [{a: 1}, {c: 3}, {b: 2, d: 4}]
  const v2 = jsSplit({a: 1, b: 2, c: 3, d: 4}, {a: true}, {c: -1}); 
  // v2 = [{a: 1}, {c: 3}, {b: 2, d: 4}]

Split Arrays

jsSplit(myArray: Array, partKeys1: Array, [partKeys2: Array, ...]): Array

result is Array that contains all parts and rest of myArray ([part1, part2, ..., rest])

  import jsSplit from 'js-split';
  const v1 = jsSplit([1,2,3,4], [0,3], [1]); 
  // v1 = [[1,4],[2],[3]]

or

jsSplit(myArray: Object, partLength: Number): Array

result is Array that contains all parts of myArray, each part is partLength size.

  import jsSplit from 'js-split';
  const v1 = jsSplit([1,2,3,4,5], 2); 
  // v1 = [[1,2], [3,4], [5]]

Split Strings

jsSplit(myText: String, partLength: Number): Array

result is Array that contains all parts of myText, each part is partLength size.

  import jsSplit from 'js-split';
  const v1 = jsSplit('Hello, people!', 3); 
  // v1 = ['Hel', 'lo,', ' pe', 'opl', 'e!']

Additional function Select

select(...) it is equal of jsSplit(...)[0] and it has been added for extra situation when you need to get only first result.

import jsSplit from 'js-split';
// ...
const firstPart = jsSplit(myObj, partKeys)[0];
// ...

OR the same result by select

import { select } from 'js-split';
// ...
const firstPart = select(myObj, partKeys);
// ...

Examples

jsSplit for String

import jsSplit from 'js-split';

jsSplit('It is text', 1); 
// = ['I', 't', ' ', 'i', 's', ' ', 't', 'e', 'x', 't']
jsSplit('It is text', 3); 
// = ['It ', 'is ', 'tex', 't']
jsSplit('It is text', 100); 
// = ['It is text']

select for String

import { select } from 'js-split';

select('It is text', 1); 
// = 'I'
select('It is text', 3); 
// = 'It '
select('It is text', 100); 
// = 'It is text'

jsSplit for Array

import jsSplit from 'js-split';

jsSplit([1,2,3,4], 2); 
// = [[1,2], [3,4]]
jsSplit([1,2,3,4], 3); 
// = [[1,2,3], [4]]
jsSplit([1,2,3,4], 4); 
// = [[1,2,3,4]]
jsSplit([1,2,3,4], 10); 
// = [[1,2,3,4]]
jsSplit([1,2,3,4], [], []); 
// = [[],[],[1,2,3,4]]
jsSplit([1,2,3,4], [1,3]); 
// = [[2,4],[1,3]]
jsSplit([1,2,3,4], ['1', '3']); 
// = [[2,4],[1,3]]
jsSplit([1,2,3,4], [1,3], [0]); 
// = [[2,4],[1],[3]]
jsSplit([1,2,3,4], [1,3], [0], [99]); 
// = [[2,4],[1],[],[3]]
jsSplit([1,2,3,4], [1,3], [0], [2], [99]); 
// = [[2,4],[1],[3],[],[]]

// you can reuse indexes
jsSplit([1,2,3,4], [1,3], [0,1]); 
// = [[2,4],[1,2],[3]]

select for Array

import { select } from 'js-split';

select([1,2,3,4], 2); 
// = [1,2]
select([1,2,3,4], 3); 
// = [1,2,3]
select([1,2,3,4], 10); 
// = [1,2,3,4]
select([1,2,3,4], [1,3]); 
// = [2,4]

jsSplit for Object

import jsSplit from 'js-split';

jsSplit({a: 1, b: 2, c: 3, d: 4}, []); 
// = [{},{a: 1, b: 2, c: 3, d: 4}]
jsSplit({a: 1, b: 2, c: 3, d: 4}, {}); 
// = [{},{a: 1, b: 2, c: 3, d: 4}]
jsSplit({a: 1, b: 2, c: 3, d: 4}, ['a'],['c']); 
// = [{a: 1},{c: 3},{b: 2, d: 4}]
jsSplit({a: 1, b: 2, c: 3, d: 4}, {a: true},{c: false}); 
// = [{a: 1},{c: 3},{b: 2, d: 4}]

// undefined values can't be added to result
jsSplit({a: 1, b: 2, c: 3, d: 4}, ['a', 'aa'],['c']); 
// = [{a: 1},{c: 3},{b: 2, d: 4}]

// you can reuse keys
jsSplit({a: 1, b: 2, c: 3, d: 4}, ['a', 'b'],['b', 'c'],['c', 'd']); 
// = [{a: 1, b: 2}, {b: 2, c: 3}, {c: 3, d: 4}, {}]

select for Object

import { select } from 'js-split';

select({a: 1, b: 2, c: 3, d: 4}, []); 
// = {}
select({a: 1, b: 2, c: 3, d: 4}, ['a', 'b']); 
// = {a: 1, b: 2}
select({a: 1, b: 2, c: 3, d: 4}, ['a', 'b', 'unreal']); 
// = {a: 1, b: 2}