loopable
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

Loopable

  • CommonJS
const { make, Loopable } = require("loopable")

const arrUsers = [
    { name: "Iron Man" },
    { name: "Black Widow" },
    { name: "Captain America" },
    { name: "Peter Parker" },
]
//Log initial
console.log(arrUsers)
/*
[
  { name: 'Iron Man' },
  { name: 'Black Widow' },
  { name: 'Captain America' },
  { name: 'Peter Parker' }
]
*/

//Make it loopable
const loopableList = make(arrUsers)
//Add modifier
loopableList
  .add((item, index) => {
      item.name = "I am " + item.name
  })
  .add((item, index) => {
      item.age = 28 - index * 2
  })
//Loop one time
const output = loopableList.loop((item, index) => {
    item.is_woman = index == 1
})
console.log(output)
/*
[
  '1.I am Iron Man, 28, false',
  '2.I am Black Widow, 26, true',
  '3.I am Captain America, 24, false',
  '4.I am Peter Parker, 22, false'
]
*/
console.log(arrUsers)
/*
[
  { name: 'I am Iron Man', age: 28, is_woman: false },
  { name: 'I am Black Widow', age: 26, is_woman: true },
  { name: 'I am Captain America', age: 24, is_woman: false },
  { name: 'I am Peter Parker', age: 22, is_woman: false }
]
*/
  • Api
  1. make(list): Loopable
  2. Loopable
  • add(modifier: Modifier): Loopable
  • loop(modifier?: Modifier): any[] //Return the list after modified

Package Sidebar

Install

npm i loopable

Weekly Downloads

2

Version

1.0.7

License

ISC

Unpacked Size

6.42 kB

Total Files

10

Last publish

Collaborators

  • mrbaobao