3.0.4 • Published 2 years ago

@unisharp/helpers.js v3.0.4

Weekly downloads
102
License
MIT
Repository
-
Last release
2 years ago

Helpers.js

Build Status Coverage Status npm version License: MIT

Installation

Via npm

npm install @unisharp/helpers.js --save

Via yarn

yarn add @unisharp/helpers.js

Getting Started

First, add our babel plugin to your bable configuration.

{
  "plugins": ["@unisharp/babel-plugin"]
}

Then require main library to happy coding.

import { Helpers } from '@unisharp/helpers.js'

Helpers.init({ global })

1.5.floor()                // 1
'Hello World'.slugify()    // 'hello-world'
[1, 2, 3].count()          // 3
[1, 2, 3].avg()            // 2
[1, 2, 2].unique()         // [1, 2]
{ a: 1, b: 2, c: 3 }.sum() // 6

Nuxt Support

Just add @unisharp/helpers.js/nuxt to the buildModules section of nuxt.config.js, and no more extra configuration is needed.

// nuxt.config.js
{
  buildModules: [
    '@unisharp/helpers.js/nuxt',
  ],
}

Available Methods

Usage

Collection

append

[1, 2, 3, 4].append(5)        // [1, 2, 3, 4, 5]
{ a: 1, b: 2 }.append(3, 'c') // { a: 1, b: 2, c: 3 }

avg

[1, 2, 3, 4, 5].avg()                     // 3
{ a: 1, b: 2, c: 3 }.avg()                // 2
[{ a: 1, b: 2 }, { a: 3, b: 4 }].avg('a') // 2

chunk

[1, 2, 3, 4, 5].chunk(2)      // [[1, 2], [3, 4], [5]]
{ a: 1, b: 2, c: 3 }.chunk(2) // [{ a: 1, b: 2 }, { c: 3 }]

contains

[1, 2, 3, 4, 5].contains(3)      // true
[1, 2, 3, 4, 5].contains(6)      // false
{ a: 1, b: 2, c: 3 }.contains(3) // true
{ a: 1, b: 2, c: 3 }.contains(4) // false

count

[1, 2, 3, 4, 5].count()      // 5
{ a: 1, b: 2, c: 3 }.count() // 3

diff

[1, 2, 3, 4, 5].diff([1, 2, 3])           // [4, 5]
{ a: 1, b: 2, c: 3 }.diff({ a: 1, b: 2 }) // { c: 3 }

diffKeys

{ a: 1, b: 2, c: 3 }.diffKeys({ a: 1, b: 2 }) // { c: 3 }

each

[1, 2, 3, 4, 5].each((value, index) => { /* ... */ })
{ a: 1, b: 2, c: 3 }.each((value, key, index) => { /* ... */ })

except

[1, 2, 3, 4, 5].except(0, 1, 2)         // [4, 5]
{ a: 1, b: 2, c: 3 }.except('a', 'b')   // { c: 3 }
[1, 2, 3, 4, 5].except([0, 1, 2])       // [4, 5]
{ a: 1, b: 2, c: 3 }.except(['a', 'b']) // { c: 3 }

fill

{ a: 1, b: 2, c: 3 }.fill(6) // { a: 6, b: 6, c: 6 }

filter

{ a: 1, b: 2, c: 3 }.filter((value, key, index) => value > 1) // { b: 2, c: 3 }

first

[1, 2, 3, 4, 5].first()                // 1
[1, 2, 3, 4, 5].first(n => n > 1)      // 2
{ a: 1, b: 2, c: 3 }.first()           // 1
{ a: 1, b: 2, c: 3 }.first(n => n > 1) // 2
flatMap
{ a: 1, b: 2 }.flatMap((value, key, index) => ({ [key]: value, [index]: value + 1 })) // { a: 1, 0: 2, b: 2, 1: 3 }

flatten

[[1, 2], [3, 4], [5]].flatten()              // [1, 2, 3, 4, 5]
{ a: { a: 1, b: 2 }, b: { c: 3 } }.flatten() // [1, 2, 3]

flip

['a', 'b', 'c'].flip()      // { a: 0, b: 1, c: 2 }
{ a: 1, b: 2, c: 3 }.flip() // { 1: 'a', 2: 'b', 3: 'c' }

freeze

['a', 'b', 'c'].freeze()      // ['a', 'b', 'c']
{ a: 1, b: 2, c: 3 }.freeze() // { a: 1, b: 2, c: 3 }

get

[1, 2, 3, 4, 5].get(2, 'default')              // 3
[1, 2, 3, 4, 5].get(5, 'default')              // 'default'
{ a: { b: { c: 1 } } }.get('a.b.c', 'default') // 1
{ a: { b: { c: 1 } } }.get('a.b.c', 'default') // 'default'

groupBy

[{ a: 'a' }, { a: 'a' }].groupBy('a')         // { a: [{ a: 'a' }, { a: 'a' }] }
{ a: { a: 'a' }, b: { a: 'a' } }.groupBy('a') // { a: { a: { a: 'a' }, b: { a: 'a' } } }

has

[1, 2, 3, 4, 5].has(2)              // true
[1, 2, 3, 4, 5].has(5)              // false
{ a: { b: { c: 1 } } }.has('a.b.c') // true
{ a: { b: { c: 1 } } }.has('a.b.d') // false

index

[1, 2, 3, 4, 5].index(3)      // 2
{ a: 1, b: 2, c: 3 }.index(2) // b

insert

[1, 2, 4, 5].insert(2, 3)          // [1, 2, 3, 4, 5]
{ a: 1, c: 3 }.insert('c', 2, 'b') // { a: 1, b: 2, c: 3 }

intersect

[1, 2, 3, 4, 5].intersect([1, 2, 3])           // [1, 2, 3]
{ a: 1, b: 2, c: 3 }.intersect({ a: 1, b: 2 }) // { a: 1, b: 2 }

intersectByKeys

{ a: 1, b: 2, c: 3 }.intersectByKeys({ a: 1, b: 2 }) // { a: 1, b: 2 }

isEmpty

[].isEmpty()                   // true
[1, 2, 3, 4, 5].isEmpty()      // false
{}.isEmpty()                   // true
{ a: 1, b: 2, c: 3 }.isEmpty() // false

isFrozen

[1, 2, 3, 4, 5].isFrozen()      // false
{ a: 1, b: 2, c: 3 }.isFrozen() // false

isNotEmpty

[].isNotEmpty()                   // false
[1, 2, 3, 4, 5].isNotEmpty()      // true
{}.isNotEmpty()                   // false
{ a: 1, b: 2, c: 3 }.isNotEmpty() // true

join

{ a: 1, b: 2, c: 3 }.join()    // 1,2,3
{ a: 1, b: 2, c: 3 }.join('-') // 1-2-3

keyBy

[{ a: 1, b: 'a' }, { a: 2, b: 'b' }].keyBy('b')         // { a: { a: 1, b: 'a' }, b: { a: 2, b: 'b' } }
{ a: { a: 1, b: 'c' }, b: { a: 2, b: 'd' } }.keyBy('b') // { c: { a: 1, b: 'c' }, d: { a: 2, b: 'd' } }

keys

{ a: 1, b: 2, c: 3 }.keys() // ['a', 'b', 'c']

last

[1, 2, 3, 4, 5].last()                // 5
[1, 2, 3, 4, 5].last(n => n < 5)      // 4
{ a: 1, b: 2, c: 3 }.last()           // 3
{ a: 1, b: 2, c: 3 }.last(n => n < 3) // 2

map

{ a: 1, b: 2, c: 3 }.map((value, key, index) => value * 2) // { a: 2, b: 4, c: 6 }

mapWithKeys

[1, 2, 3].mapWithKeys((value, index) => ({ [value]: index }))               // { 1: 0, 2: 1, 3: 2 }
{ a: 1, b: 2, c: 3 }.mapWithKeys((value, key, index) => ({ [value]: key })) // { 1: 'a', 2: 'b', 3: 'c' }

max

[1, 2, 3, 4, 5].max()      // 5
{ a: 1, b: 2, c: 3 }.max() // 3

merge

[1, 2, 3, 4, 5].merge([6, 7])              // [1, 2, 3, 4, 5, 6, 7]
{ a: 1, b: 2, c: 3 }.merge({ d: 4, e: 5 }) // { a: 1, b: 2, c: 3, d: 4, e: 5 }

min

[1, 2, 3, 4, 5].min()      // 1
{ a: 1, b: 2, c: 3 }.min() // 1

only

[1, 2, 3, 4, 5].only(0, 1, 2)         // [1, 2, 3]
{ a: 1, b: 2, c: 3 }.only('a', 'b')   // { a: 1, b: 2 }
[1, 2, 3, 4, 5].only([0, 1, 2])       // [1, 2, 3]
{ a: 1, b: 2, c: 3 }.only(['a', 'b']) // { a: 1, b: 2 }

partition

[1, 2, 3, 4, 5].partition((value, key) => key < 2 && value < 3)             // [[1, 2], [3, 4, 5]]
{ a: 1, b: 2, c: 3 }.partition((value, key) => key === 'a' && value === 1)) // [{ a: 1 }, { b: 2, c: 3 }]

pipe

[1, 2, 3, 4, 5].pipe(items => [...items, 6])           // [1, 2, 3, 4, 5, 6]
{ a: 1, b: 2, c: 3 }.pipe(items => { ...items, d: 4 }) // { a: 1, b: 2, c: 3, d: 4 }

pluck

[[1, 2, 3], [1, 2, 3]].pluck(0)                      // [1, 1]
[{ a: 1, b: 2 }, { a: 1, b: 2 }].pluck('a')          // [1, 1]
{ a: { a: 1, b: 2 }, b: { a: 1, b: 2 } }.pluck('a')  // [1, 1]
[{ a: 1, b: 'a' }, { a: 2, b: 'b' }].pluck('a', 'b') // { a: 1, b: 2 }

prepend

[2, 3, 4, 5].prepend(1)        // [1, 2, 3, 4, 5]
{ b: 2, c: 3 }.prepend(1, 'a') // { a: 1, b: 2, c: 3 }

reduce

{ a: 1, b: 2, c: 3 }.reduce((carry, value, key, index) => carry + value) // 6

reject

[1, 2, 3, 4, 5].reject((value, index) => value > 4)           // [1, 2, 3, 4]
{ a: 1, b: 2, c: 3 }.reject((value, key, index) => value > 2) // { a: 1, b: 2 }

reverse

{ a: 1, b: 2, c: 3 }.reverse() // { c: 3, b: 2, a: 1 }

set

[1, 2, 0, 4, 5].set(2, 3)              // [1, 2, 3, 4, 5]
{ a: { b: { c: 0 } } }.set('a.b.c', 1) // { a: { b: { c: 1 } } }

shuffle

[1, 2, 3, 4, 5].shuffle() // [3, 5, 1, 4, 2]

slice

{ a: 1, b: 2, c: 3 }.slice(1)     // { b: 2, c: 3 }
{ a: 1, b: 2, c: 3 }.slice(-1)    // { c: 3 }
{ a: 1, b: 2, c: 3 }.slice(1, 2)  // { b: 2 }
{ a: 1, b: 2, c: 3 }.slice(1, -1) // { b: 2 }

sort

{ a: 3, b: 2, c: 1 }.sort()                     // { c: 1, b: 2, a: 3 }
{ a: [3, 3], b: [3, 2] }.sort()                 // { b: [3, 2], a: [3, 3] }
{ a: { a: 3, b: 3 }, b: { a: 3, b: 2 } }.sort() // { b: { a: 3, b: 2 }, a: { a: 3, b: 3 } }
{ a: 1, b: 2, c: 3 }.sort((a, b) => b - a)      // { c: 3, b: 2, a: 1 }

sortDesc

[1, 2, 3, 4, 5].sortDesc()                          // [5, 4, 3, 2, 1]
[[5, 4], [5, 5]].sortDesc()                         // [[5, 5], [5, 4]]
[{ a: 5, b: 4 }, { a: 5, b: 5 }].sortDesc()         // [{ a: 5, b: 5 }, { a: 5, b: 4 }]
{ a: 1, b: 2, c: 3 }.sortDesc()                     // { c: 3, b: 2, a: 1 }
{ a: [3, 2], b: [3, 3] }.sortDesc()                 // { b: [3, 3], a: [3, 3] }
{ a: { a: 3, b: 2 }, b: { a: 3, b: 3 } }.sortDesc() // { b: { a: 3, b: 3 }, a: { a: 3, b: 2 } }

sortBy

[[5, 5], [5, 4]].sortBy(1)                              // [[5, 4], [5, 5]]
[{ a: 5, b: 5 }, { a: 5, b: 4 }].sortBy('b')            // [{ a: 5, b: 4 }, { a: 5, b: 5 }]
{ a: [3, 3], b: [3, 2] }.sortBy(1)                      // { b: [3, 2], a: [3, 3] }
{ a: { a: 3, b: 3 }, b: { a: 3, b: 2 } }.sortBy('b')    // { b: { a: 3, b: 2 }, a: { a: 3, b: 3 } }
[[5, 5], [5, 4]].sortBy(item => item[1])                // [[5, 4], [5, 5]]
[{ a: 5, b: 5 }, { a: 5, b: 4 }].sortBy(item => item.b) // [{ a: 5, b: 4 }, { a: 5, b: 5 }]

sortByDesc

[[5, 4], [5, 5]].sortByDesc(1)                              // [[5, 5], [5, 4]]
[{ a: 5, b: 4 }, { a: 5, b: 5 }].sortByDesc('b')            // [{ a: 5, b: 5 }, { a: 5, b: 4 }]
{ a: [3, 2], b: [3, 3] }.sortByDesc(1)                      // { b: [3, 3], a: [3, 2] }
{ a: { a: 3, b: 2 }, b: { a: 3, b: 3 } }.sortByDesc('b')    // { b: { a: 3, b: 3 }, a: { a: 3, b: 2 } }
[[5, 4], [5, 5]].sortByDesc(item => item[1])                // [[5, 5], [5, 4]]
[{ a: 5, b: 4 }, { a: 5, b: 5 }].sortByDesc(item => item.b) // [{ a: 5, b: 5 }, { a: 5, b: 4 }]

sum

[1, 2, 3, 4, 5].sum()                     // 15
{ a: 1, b: 2, c: 3 }.sum()                // 6
[{ a: 1, b: 2 }, { a: 3, b: 4 }].sum('a') // 4

swap

[1, 2, 3, 4, 5].swap(0, 1)          // [2, 1, 3, 4, 5]
{ a: 1, b: 2, c: 3 }.swap('a', 'b') // { a: 2, b: 1, c: 3 }

take

[1, 2, 3, 4, 5].take(2)      // [1, 2]
{ a: 1, b: 2, c: 3 }.take(2) // { a: 1, b: 2 }

toArray

{ a: { a: 1, b: 2 }, b: { c: 3 }}.toArray() // [1, 2, 3]

unique

[1, 1, 2, 2, 3].unique()      // [1, 2, 3]
{ a: 1, b: 2, c: 2 }.unique() // { a: 1, b: 2 }

values

{ a: 1, b: 2, c: 3 }.values() // [1, 2, 3]

String

camel

'hello-world'.camel() // 'helloWorld'

kebab

'Hello World'.kebab() // 'hello-world'

lcfirst

'Hello World'.lcfirst() // 'hello World'

limit

'Hello World'.limit(5)        // 'Hello...'
'Hello World'.limit(5, '***') // 'Hello***'

nl2br

'Hello\nWorld'.nl2br() // 'Hello<br>World'

random

String.random()   // 'hnq6dmd5f9keo20d'
String.random(10) // 'erbzjw0rks'

slugify

'Hello World'.slugify() // 'hello-world'

snake

'Hello World'.snake() // 'hello_world'

stripTags

'<h1>Hello World</h1>'.stripTags() // 'Hello World'

studly

'hello-world'.studly() // 'HelloWorld'

title

'hello-world'.title() // 'Hello World'

ucfirst

'hello world'.ucfirst() // 'Hello world'

Number

abs

(-10).abs() // 10

ceil

1.4.ceil() // 2

downto

(5).downto(1, n => n) // [5, 4, 3, 2, 1]

floor

1.5.floor() // 1

format

(1000000).format() // '1,000,000'

random

Number.random()    // 0.6310469770350284
Number.random(5)   // 4
Number.random(5.5) // 2.9265187166993445

round

1.44.round()  // 1
1.44.round(1) // 1.4

times

(5).times(n => n) // [1, 2, 3, 4, 5]

upto

(1).upto(5, n => n) // [1, 2, 3, 4, 5]

Date

(1).year().ago()
(10).days().ago()
(1).hour().after()
(10).minutes().after(new Date(2000, 1, 1, 1, 1, 1, 1))

License

MIT

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.1.1

2 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.2.25

3 years ago

1.2.26

3 years ago

1.2.23

3 years ago

1.2.24

3 years ago

1.2.21

3 years ago

1.2.22

3 years ago

1.2.20

3 years ago

1.2.19

3 years ago

1.2.18

4 years ago

1.2.17

4 years ago

1.2.16

4 years ago

1.2.14

4 years ago

1.2.15

4 years ago

1.2.13

4 years ago

1.2.12

4 years ago

1.2.11

5 years ago

1.2.10

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.12.5

6 years ago

0.12.4

6 years ago

0.12.3

6 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.0

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.0

6 years ago

0.7.4

6 years ago

0.7.3

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago