🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

array-iter-next-crnt

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-iter-next-crnt

Get array items one-by-one and remember the current one.

latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
49
1533.33%
Maintainers
1
Weekly downloads
 
Created
Source

array-iter-next-crnt

Get array items one-by-one and remember the current one.

Usage

from test/take2dict.js:

var iter = require('array-iter-next-crnt'), list, it, dict,
  expectEqual = require('equal-pmb'), expected;

list = [ 'animals', 3, 'bird', 'cat', 'horse',
  'tooFew', 9001,
  'empty', 0,
  'alpha', 5, 'a', 'b', 'c',    // misleading notation in order to
  'nums', 0, 1, 2, 3, 4,        // show that it doesn't matter
  'fruit', 2, 'banana', 'pear',
  ];

it = iter(list);
dict = {};
it.each(function (key) {
  expectEqual(it, this);
  expectEqual(it.crnt, key);
  var nextUp = it.peek(), howMany;
  expectEqual(it.next(), it);
  howMany = it.crnt;
  expectEqual(nextUp, howMany);
  dict[key] = it.take(howMany);
});

expected = {
  animals: [ 'bird', 'cat', 'horse' ],
  tooFew: false,
  empty: [],
  alpha: [ 'a', 'b', 'c', 'nums', 0 ],    // took 5, as expected.
  '1': [ 3, 4 ],
  fruit: [ 'banana', 'pear' ],
};
expectEqual(dict, expected);

In scenarios where you know that all your values are truthy and you don't mind coding style:

while (x = myIter()) { console.log(x); }

Known issues

  • needs more/better tests and docs

 

License

ISC

Keywords

array

FAQs

Package last updated on 11 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts