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

hathor-utils

v0.0.3

Published

Utilities for use by Hathor and its plugins.

Downloads

2

Readme

Hathor Utils

Collection of utility functions for use with Hathor projects, plugins, and routes. The basic usage of each method is covered here, but full usage is covered in the tests.

NOTE: All functions in this library work in an immutable fashion, creating clones or copies of objects and never modifing the source object or values.

Install

npm install --save hathor-utils

API

noop()

An empty function, no operation, useful for things like optional callbacks.

stringify(obj, serializer, indent, decycler)

Wrapper around json-stringify-safe, useful when you want to JSON.stringify an object that may or may not have things that are compatable with JSON.

isHtmlPage(pageName)

Returns a true false flag if the string filename passed in ends in html or htm.

isTrue(value)

Checks value to see if it is either "true" or a boolean true value. If so will return a boolean true. All other values return a boolean false.

isFalse

Checks value to see if it is either "false" or a boolean false value. If so will return a boolean true. All other values return a boolean false.

isNumeric

Checks value to see if a value is a numeric value. If so will return a boolean true, if the value is a string that represents a numeric true will also be returned. All other values return a boolean false.

exclude(source, [key], [...key])

Creates a new object then copies all key values from source into the new object and excludes any keys provided.

unique(array)

Returns a new array that only contains unique values from the source aray. Useful to dedupe arrays.

decode64(str)

Decodes a Base 64 string into a binary blob.

encode64(blob)

Encodes a value into a Base 64 string representation.

keyToPath(key, splitOn = /[./]/)

Takes key as a string and splits it on the expression provided (defaults to splitting on all .'s and /'s) then returns the resulting array. Useful for passing search paths to getObjectValue, setObjectValue, etc...

getObjectValue(path, obj, defaultValue)

Accepts path as an array of keys to recurse through and return the value from obj. If the value is not found then returns defaultValue (undefined by default).

Example:

const src = {
  foo: {
    bar: {
      value: 'something'
    }
  }
};
const value = getObjectValue(keyToPath('foo.bar.value'),  src, 'value');
const noValue = getObjectValue(keyToPath('foo.bar.noValue'), src, 'nothing');
console.log(value); // outputs "something"
console.log(noValue); // outputs "nothing"

setObjectValue(path, obj, value)

Accepts path as an array of keys to recurse through the keys creating new objects if no key exists and updating the final value.

Example:

const src = {
  foo: {
  }
};
const out1 = setObjectValue(keyToPath('foo'),  src, 'bar');
const out2 = setObjectValue(keyToPath('foo.bar'), src, 'value');
console.log(out1); // outputs {foo: "bar"}
console.log(out2); // outputs {foo: {bar: "value"}}

removeObjectValue(path, obj)

Similar to exclude, but instead works with key paths and can remove embedded values from the source object.

Example:

const src = {
  foo: {
    bar: {
      value: 'something'
    }
  }
};
const value = removeObjectValue(keyToPath('foo.bar.value'),  src);
console.log(value); // {foo: bar: {}}

merge(source, [obj], [obj], ...)

Creates a deep clone of source and merges the values from each additional argument into the new object.

Example:

const src = {foo: 'bar'};
const out = merge(src, {bar: 'none'}, {some: 'value'});
console.log(out); // {foo: 'bar', bar: 'none', some: 'value'}

clone(source)

Creates a deep clone of source and returns it.

If the passed value is a Date or Regular Expression a new instance is created and returned. If the value is a string, numeric, or boolean then it is returned as by default these do not require explicit cloning. If the value is an array it is mapped over returning a new array of cloned values within. If the value is an object then all keys are mapped over, checked against "hasOwnProperty" and cloned recursivly then returned.

toUnderscore

Converts a camel cased string into an underscored uppercase string.

Example:

const input = 'testString';
const output = toUnderscore(input);
console.log(output); // TEST_STRING

underscoreKeys

Takes an object, itterates over its keys calling toUnderscore recursivly, and returns a new object with the keys in all uppercase with underscores.

camelCase

Converts a string into its camel cased representation.

const input = 'TEST_STRING FOO-BAR_NONE';
const output = camelCase(input);
console.log(output); // testStringFooBarNone

camelKeys

Takes an object, itterates over its keys calling camelCase recursivly, and returns a new object with the keys converted to camel case.