JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 59
  • Score
    100M100P100Q56885F
  • License MIT

Package Exports

  • reiterator
  • reiterator/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (reiterator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ReIterator is a node.js module which helps you avoid errors while accessing json from the big bad internet (and your own apis)

About

I created this package to simplify iterating across arrays and objects without hitting into errors. The Iterator is chainable, making it possible to access items as $$(obj)._('first')._(0)._('second).value()

Quick Start

yarn add reiterator
const i = require("reiterator");

// This is our object
const json = {
  name: { firstName: "Jack", lastName: "Reaper" },
  city: "Jakarta",
  runs: [
    { run1: { date: "26 March 2020", distance: "5km" } },
    { run2: { date: "20 March 2020", distance: "5km" } },
  ],
  active: true,
  waiting: null,
  countries: undefined,
};

// And this is how you can parse through it

i.$$(json)._("name")._("firstName").string(); // "Jack"
i.$$(json)._("runs")._(0)._("run1")._("distance").string(); // "5km"
i.$$(json)._("runs").keys(); // ["run1", "run2"]
i.$$(json)._("name").value(); // { firstName: "Jack", lastName: "Reaper" }

i.$$(json)._("name"); // { obj: { firstName: "Jack", lastName: "Reaper" } }

i._keys(json.runs); // ["run1", "run2"]
i._len(json.runs); // 2

i.isUndefined(json.countries); // true

The Complete API

$ and _ are aliases $$(json) and new Iterator(json) are aliases

.keys() always returns an array or [] .string() always returns an string or ""

const json = { one: "two" };

const Iterator = require("reiterator");

const i = new Iterator(json);
const ii = Iterator.$$(json); // alias

i.$("one"); // { obj: "two" }
i.$("one", true); // "two"

i._("one"); // { obj: "two" } alias
i._("one", true); // "two" alias

i.keys(); // ["one"]
Iterator.keys(json); // ["one"]

i.$("one").string(); // "two"
Iterator._str(json.one); // "two" alias

i.value(); // { "one": "two" }

i.length(); // 1
Iterator._len(json); // 1

Iterator.hasKey(json, "one"); // true
Iterator.hasKey(json, "two"); // false

// Other static functions
Iterator.isArray(json); // false
Iterator.isObject(json); // true
Iterator.isNumber(json); // false
Iterator.isString(json); // false
Iterator.isBoolean(json); // false
Iterator.isNull(json); // false
Iterator.isUndefined(json); // false