You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

seqit

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

seqit

Iterate over anything, in a style reminiscent of C++.

Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

seqit - Sequential Iterator

Sometimes the JavaScript forEach loop is not dynamic enough, especially when you have to break out of the loop early, visit elements in a different order, or modify the object you are iterating over in-place. And functions like map or reduce and many of the functions provided by a library like lo-dash can be easily accomplished using a traditional loop without extra abstraction. This is where seqit comes in.

seqit is a general-purpose iterator module incompatible with both Node.js and the browser, designed to provide a simple method of looping over an array or object using for or while, inspired by C++ iterators. You can wrap any array or object with the seqit function and iterate over its elements or keys. You can also edit the values you're iterating over in-place. seqit does not copy any data; it simply binds to an existing structure.

Usage

var seqit = require('seqit');
var array = [5, 4, 3, 5, 6, 7, 8, 9, 10];
for (var it = seqit(array); it !== it.end; it.next) {
  console.log(it());
}

Equivalently, in a while loop,

var it = seqit(array)
while (it.hasNext) {
  console.log(it.next);
}

Looping over an object:

var obj = { a: 5, b: 'woop' };
for (var it = seqit(obj); it !== it.end; it.next) {
  console.log(it().key + ': ' + it().value);
}

Editing an object in place:

var obj = { a: 'woop', b: 'doop' }
for (var it = seqit(obj); it !== it.end; it.next) {
  it().key = it().key + 'a';
  it().value = it().value + 'a';
}
console.log(obj);
// { aa: 'woopa', ba: 'doopa' }

Editing an array in place:

var array = [5, 4, 3, 5, 6, 7, 8, 9, 10];
for (var it = seqit(array); it !== it.end; it.next) {
  it(11);
}
console.log(array);
// [11, 11, 11, 11, 11, 11, 11, 11, 11]

Using the iteration index:

var array = [5, 4, 3, 5, 6, 7, 8, 9, 10];
for (var it = seqit(array); it !== it.end; it.next) {
  console.log(it.index);
}
// 0, 1, 2, 3 ...

You can also set it.index if you want to manually change where you are in the loop. Using it.begin

for (var it = seqit(array); it !== it.end; it.next) {
  if (it.begin === it) {
    console.log('iterating over the first element of the array');
  }
}

More examples to come...

Keywords

seqit

FAQs

Package last updated on 29 Jul 2015

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