@youngbeen/es-halfmoon

0.1.2 • Public • Published

[TOC]

ES-halfmoon

New ES6+ has many amazing features like filter, map etc. But it's not enough. ES-halfmoon is about to provide some prototype methods for ES not covered yet.

All features will only be attached when ES does not provide, ES-halfmoon never overrides native ES methods

Features

  • Array.get() like map
  • Array.forEachRight() loop reversed

Install

Install it via npm

npm i @youngbeen/es-halfmoon

Usage

Array.get()

Array.get(value, [keyName])

Tip: if keyName not set, default get priority is id > key > value

e.g.

const fruits = [
  { id: 'apple', name: 'Apple' },
  { id: 'orange', name: 'Orange' },
  { id: 'banana', name: 'Banana' }
]
// finding item by specific value
fruits.get('orange') // -> { id: 'orange', name: 'Orange' }
// which equivalent
fruits.get('orange', 'id')

const myFruits = [
  { type: 'apple', name: 'Apple' },
  { type: 'orange', name: 'Orange' },
  { type: 'banana', name: 'Banana' }
]
// specify keyName
fruits.get('orange', 'type')

Array.forEachRight()

Array.forEachRight(callback(item, [index]))

This method is useful when you are looping array for removing some item

e.g.

const array = ['h', 'e', 'l', 'l', 'o']
array.forEachRight((item, index) => {
  console.log(item, index)
})
// 'o' 4
// 'l' 3
// 'l' 2
// 'e' 1
// 'h' 0

Package Sidebar

Install

npm i @youngbeen/es-halfmoon

Weekly Downloads

1

Version

0.1.2

License

ISC

Unpacked Size

5.6 kB

Total Files

5

Last publish

Collaborators

  • youngbeen