Giter Site home page Giter Site logo

javascript-extensions's Introduction

npm License: MIT CI Status

Javascript Extensions

Useful javascript/typescript extensions

Installation

Npm:

$ npm install --save @nbottarini/javascript-extensions

Yarn:

$ yarn add @nbottarini/javascript-extensions

Then simply import the module from the main javascript/typescript file of your application or library:

import '@nbottarini/javascript-extensions'

Array Extensions

[1, 2, 2, 3].distinct() // returns [1, 2, 3]
[1, 2, 3].isEmpty() // returns false
[1, 2, 3].isNotEmpty() // returns true
    
[1, 2, 3].firstOrNull() // returns 1
[].firstOrNull() // returns null
[1, 2, 3].firstOrNull(i => i > 1) // returns 2
[1, 2, 3].firstOrNull(i => i > 5) // returns null

[1, 2, 3].first() // returns 1
[].first() // returns undefined
[1, 2, 3].first(i => i > 1) // returns 2
[1, 2, 3].first(i => i > 5) // returns undefined

[1, 2, 3].lastOrNull() // returns 3
[].lastOrNull() // returns null
[1, 2, 3].lastOrNull(i => i < 3) // returns 2
[1, 2, 3].lastOrNull(i => i > 5) // returns null

[1, 2, 3].last() // returns 3
[].last() // returns undefined
[1, 2, 3].last(i => i < 3) // returns 2
[1, 2, 3].last(i => i > 5) // returns undefined

[1, 2, 3].contains(2) // returns true
[1, 2, 3].contains(4) // returns false

[3].single() // returns 3
[].single() // throws error
[1, 2, 3].single(i => i > 2) // returns 3
[1, 2, 3].single(i => i > 1) // throws error
[1, 2, 3].single(i => i > 3) // throws error

[3].singleOrNull() // returns 3
[].singleOrNull() // returns null
[1, 2, 3].singleOrNull(i => i > 2) // returns 3
[1, 2, 3].singleOrNull(i => i > 1) // returns null
[1, 2, 3].singleOrNull(i => i > 3) // returns null

[1, 2, 3].all(i => i > 0) // returns true
[1, 2, 3].all(i => i > 3) // returns false

[1, 2, 3].any(i => i > 1) // returns true
[1, 2, 3].any(i => i > 3) // returns false

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

[1, 2, 3].takeLast(1) // returns [3]
[1, 2, 3, 4, 5].takeLast(3) // returns [3, 4, 5]
    
[1, 2, 3].drop(1) // returns [2, 3]
[1, 2, 3, 4, 5].drop(3) // returns [4, 5]

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

[1, 2, 3].forEachIndexed((item, index) => {
    // Do something
})

[1, 2, 3].mapIndexed((item, index) => {
    // Map the array to other array
})

[1, 2, 3].max() // returns 3
['a', 'd', 'b'].max() // returns 'd'
[].max() // returns undefined

[{ age: 13 }, { age: 20}, { age: 6}].maxOf(e => e.age) // returns 20
    
[1, 2, 3].min() // returns 1
['a', 'd', 'b'].min() // returns 'a'
[].min() // returns undefined

[{ age: 13 }, { age: 20}, { age: 6}].minOf(e => e.age) // returns 6

String Extensions

'23'.padZeros(3) // returns '023'
'23'.padZeros(5) // returns '00023'
'23'.padZeros(1) // returns '23'

'hello'.removePrefix('h') // returns 'ello'
'hello'.removePrefix('he') // returns 'llo'
'hello'.removePrefix('by') // returns 'hello'

'hello'.countOccurrences('he') // returns 1
'hello'.countOccurrences('l') // returns 2
'hello'.countOccurrences('w') // returns 0

javascript-extensions's People

Contributors

nbottarini avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.