npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saunter

v0.11.0

Published

Walk through data. Do things as you go.

Downloads

16

Readme

Saunter

Walk through data. Do things as you go.

Install

npm install saunter

Usage

Saunter is meant to be used with libraries like Ramda, Transducers.js, or Lodash.

walk

The walk function takes an object or array and walks through it. Calling walk will return a generator. The generator yields a walkObject for every value it finds with the following properties.

The walkObject signature:

  • path (array) - The path to the value. It will be an array of object properties and array indexes.
  • value (any) - The value for the given path.

Note: this will find every value, even objects, and then it will return the values of those objects. The same is true for arrays. To remove these, filter out objects and array.

If walk gets a string, number, or boolean, it will yield a single object.

Arguments

walk: (any) -> array[walkObject]

walkObject: {path, value}
path: array[string | number]
value: any

The walk function takes any kind of value and returns an array of the walkObject.

Example

const { walk } = require("saunter");
const subject = {
  name: "Jane Doe",
  email: "[email protected]",
  address: {
    city: "New York",
    state: "New York",
    zip: "10101",
  },
};
const walker = walk(subject);
console.log([...walker]);

This prints:

[
  { path: ["name"], value: "Jane Doe" },
  { path: ["email"], value: "[email protected]" },
  { path: ["address", "city"], value: "New York" },
  { path: ["address", "state"], value: "New York" },
  { path: ["address", "zip"], value: "10101" }
]

updateWalk

This walks through an object or an array, looks for a match, calls a handler, and replaces the value with the returned value. The purpose for this function is so you can walk through data and make updates as you go.

Arguments

See walk function for walkObject definition.

updateWalk: (subject, array[matcher]) -> any

subject: any
matcher: {match, handle}
match: (walkObject, subject) -> boolean
handle: (walkObject, subject) -> any
remove: (walkObject, subject) -> boolean

The subject is passed into the match and handle functions to allow you to get other information, such as parents or children. If a remove function is given, it will remove the values if the function returns true.

Example

This example walks over an object, looks for even numbers, and when found, multiples them by 10.

const { updateWalk } = require("saunter");
const subject = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
};
const result = updateWalk(subject, [
  {
    match: ({ value }) => value % 2 === 0,
    handle: ({ value }) => value * 10,
  },
]);
console.log(result);

This prints:

{
  a: 1,
  b: 20,
  c: 3,
  d: 40
}

pathMatch

Function for matching paths. This can be used for filtering or searching through data.

Arguments

The pathMatch function will return a function that takes a walkObject.

pathMatch: (array[string | integer | check]) -> (walkObject) -> boolean

check: (string | integer) -> boolean

The function can take an array of three types of values. A check function can be provided as a way to do logic during a check.

Examples

Matching strings and integers:

const { pathMatch } = require("saunter");
const matcher = pathMatch(["foo", 1, "bar"]);
console.log(matcher(["foo", 1, "bar"]));
// prints true

Matching with functions:

const { pathMatch } = require("saunter");
function isOdd(value) {
  return value % 2 !== 0;
}
const matcher = pathMatch(["foo", isOdd]);
console.log(matcher(["foo", 1]));
// prints true

pathStartsWith

Similar to pathMatch, but only checks to see if the given path starts with the pattern given.

Arguments

Same as pathMatch.

Example

const { pathStartsWith } = require("saunter");
const matcher = pathStartsWith(["foo"]);
console.log(matcher(["foo", 1, "bar"]));
// prints true

getPath

Helper function to get the path from the walkObject.

Arguments

See walk function for walkObject definition.

getPath: (walkObject) -> path

Example

const { getPath } = require("saunter");
getPath({ path: ["foo", 1], value: 42 });
// returns ["foo", 1]

getValue

Helper function to get the value from the walkObject.

Arguments

getValue: (walkObject) -> any

Example

const { getValue } = require("saunter");
getValue({ path: ["foo", 1], value: 42 });
// returns 42