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

raskolnikov

v0.2.0

Published

Existential operations

Downloads

3

Readme

Raskolnikov npm badge travis

"I want to attempt a thing like that and am frightened by these trifles."

Motivation

As many ex-CoffeeScripters (myself included) return to JavaScript, one of more missed features is the existential ? operator which allowed for inline existence chaining. For example:

sparce?.field1?.field2?.function?(params)

While arguably it can lead to bad practices and habits, it was still very convenient. raskolnikov is not meant to be a replacement, but does supply some similar functionality.

The JavaScript equivalent of the above using raskolnikov would be:

Existential.from(sparce)
  .exists('field1')
  .exists('field2')
  .exists('function')
  .exists(fn => fn(params))
  .val();

Install

$ npm install raskolnikov

Usage

import Existential from 'raskolnikov';

const object = {
  hello: [
    world(greeting) {
      return `${greeting} world`;
    }
  ]
};

Existential.from(object)
  .exists('hello')
  .exists(0)
  .exists('world')
  .exists(fn => fn('Hello'))
  .val(); // Returns 'Hello world'

Existential.from(object)
  .exists('goodbye')
  .exists('crime')
  .exists(val => `Is there ${val}?`)
  .val(); // Returns null

Methods

raskolnikov can be initialized using:

import Existential from 'raskolnikov';

const existential = new Existential(value);
// or
const existential = Existential.from(value);

The check for existance is done through:

function exists(value) {
  return typeof value !== 'undefined' && value === null;
}

exists(key)

Chain of existential type checking. key can be a string, a number, an array of string and numbers, or a function. The return value is a new Existential instance.

String

Accesses the field of the previous object: previous[string]

Number

Accesses the index of the previous value: previous[number]

Array

Accesses the field (if string) or the index (if number) in sequence. The following two are therefore equivalent:

Existential.from(initialValue)
  .exists('answer')
  .exists(42)
  .exists('universe')

// and

Existential.from(initialValue)
  .exists(['answer', 42, 'universe'])

Function

Evaluates the function given the previous value. The typical use case is would be check if an object has a certain method then using this to evaluate the function:

function hello(greeting) {
  return `${greeting} world`;
}

Existential.from({hello})
  .exists('hello')
  .exists(fn => hello('Hola'))
  .val(); // Return 'Hola world';

However, any function can be used and the available parameter will be the previous value.

Existential.from({hello: 'world'})
  .exists('hello')
  .exists(val => `hello ${val}`)
  .val(); // Return 'hello world';

$(key)

Shorthand for exists(val). So the following are equivalent:

```javascript` Existential.from(object) .exists('hello') .exists(0) .exists('world') .exists(fn => fn('Hello'))

Existential.from(object) .$('hello') .$(0) .$(world) .$(fn => fn('Hello'))

// or using array access Existential.from(object) .$(['hello', 0, 'world') .$(fn => fn('Hello'))


## val()
Ends the existential chain and returns the final value. If at any point the
value did not exists, the chain is short-circuited and will return `null`.


## or(fallback)
Like the `val()` method, this will ends the existential chain and returns the
final value if it exists, otherwise return the `fallback`.

## version
Returns the current version of `raskolnikov`.


# License
Copyright © 2015 Nicholas Hwang - [MIT License](LICENSE)