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

railsish

v0.9.0

Published

Rails-like blank and presence helpers, with related property access methods

Downloads

9

Readme

import { strict: assert } from "assert";
import { isPresent, isBlank } from "railsish";

// It treats empty or invalid values as blank...
assert(isBlank([]), "an empty array is blank");
assert(isBlank({}), "an empty object is blank");
assert(isBlank(new Date("2020-03-32")), "an invalid date is blank");
assert(isBlank(NaN), "NaN is blank");
assert(isBlank("\n\t  "), "a string containing only whitespace is blank");

// But there's a special case: 0 is present!
assert(isPresent(0), "0 is present");
import { strict: assert } from "assert";
import { getPresent } from "railsish";

assert.equal(getPresent({ foo: { bar: { baz: null } } }, "foo.bar.baz", "quux"), "quux", "it will return a default value");
import { strict: assert } from "assert";
import { getPresentNumber, getPresentString } from "railsish";

assert.equal(getPresentNumber({ foo: "-3.14" }, "foo"), -3.14, "fetching a numeric string will cast it");
assert.equal(getPresentString({ foo: "\t" }, "foo", "default"), "default", "whitespace-only strings are not present");
import { booleanize } from "railsish";

booleanize(0) === false
booleanize("0") == false
// an empty object
booleanize({}) === false
// an empty array
booleanize([]) === false
booleanize(new Int8Array()) === false
// an empty string
booleanize("") === false
// a string that's only whitespace
booleanize("    ") === false
// special string handling: the following are case-insensitive
booleanize("F") === false
booleanize("n") === false
booleanize("no") === false
booleanize("nil") === false
booleanize("NULL") === false
booleanize("FALSE") === false
import { strict: assert } from "assert";
import { getBoolean } from "railsish";

assert.fail(getBoolean({ foo: [] }, "foo"), "it will booleanize the value at a given path");
booleanize(0) === false
booleanize("0") == false
// an empty object
booleanize({}) === false
// an empty array
booleanize([]) === false
booleanize(new Int8Array()) === false
// an empty string
booleanize("") === false
// a string that's only whitespace
booleanize("    ") === false
// special string handling: the following are case-insensitive
booleanize("F") === false
booleanize("n") === false
booleanize("no") === false
booleanize("nil") === false
booleanize("NULL") === false
booleanize("FALSE") === false
import { getPresentArray }
const response = {
  foo: {
    bar: {
      baz: {
        quux: []
      }
    }
  }
};

// instead of this:
function extractDeepValue(value) {
  if (value && value.foo) {
    if (value.foo.bar) {
      if (value.foo.bar.baz) {
        if (Array.isArray(value.foo.bar.baz.quux) && value.foo.bar.baz.quux.length > 0) {
          return value;
        }
      }
    }
  }

  return ["default"];
};

extractDeepValue(response, ["default"]);

// you can just:
getPresentArray(response, "foo.bar.baz.quux", ["default"]);
import { getPresentArray } from "railsish";

const NO_TAGS_SELECTED = Symbol("NO_TAGS");

const getSelectedTags = (config) => getPresentArray(config, "user.selected.tags", NO_TAGS_SELECTED);

// A contrived express handler
export async function renderSelectedTags(req, res) {
  const tags = getSelectedTags(req.user.config);

  if (tags === NO_TAGS_SELECTED) {
    // take the user to a 
    return res.json({
      error: "User needs to select tags",
      code: "SELECT_TAGS_FIRST"
    }).status(400);
  }

  const tagResponse = await myDB.getTags(tags);

  res.json(tagResponse);
}
// It'll booleanize a deeply-nested value
getBoolean({ foo: { bar: [] } }, "foo.bar") === false;
getPresentString({ foo: "\t" }, "foo", "default") === "default";
isPresentObject({}) === false
isPresentObject({ foo: "bar" }) === true
isPresentObject([]) === false
isPresentObject(new Foo()) === false
isPresentObject(Object.create(null)) === false
isBlankObject({}) === true
isBlankObject({ foo: "bar" }) === false
isBlankObject([]) === false
isBlankObject(new Foo()) === false
isBlankObject(Object.create(null)) === false
import { install as installRailsishMatchers } from "railsish/jest-matchers"

installRailsishMatchers(expect)

describe("some tests", () => {
  it("returns a present response", () => {
    expect(myLibrary.doSomething()).toBePresent();
  });

  it("returns something blank", () => {
   expect(anonymousRequest.getCurrentUser()).toBeBlank();
  });

  it("implements foo", () => {
    expect({ foo: () => "bar" }).toRespondTo("foo");
  });
});
// in a file called by "setupFilesAfterEnv":
import { install } from "railsish/jest-matchers"

install(expect);
expect(0).toBePresent();
expect([]).not.toBePresent();
expect(false).not.toBePresent();
expect("some string").toBePresent();
expect({}).not.toBePresent();
expect({ foo: "bar" }).toBePresent();
expect
expect(0).not.toBeBlank();
expect([]).toBeBlank();
expect({}).toBeBlank();
expect(NaN).toBeBlank();
expect(Infinity).not.toBeBlank();
expect("\t\n\t").toBeBlank();
expect("some text").toBooleanizeAs(true);
expect([]).toBooleanizeAs(false);
expect("some text").toBooleanizeTrue();
expect([]).not.toBooleanizeTrue();
expect("some text").not.toBooleanizeFalse();
expect([]).toBooleanizeFalse();
expect(new Date()).toRespondTo("toISOString");
expect({ foo: { bar: { baz: () => "quux" } } }).toRespondToPath("foo.bar.baz");