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

keystone-utils

v0.4.0

Published

A useful library of utilities used by KeystoneJS, and you!

Downloads

7,172

Readme

Keystone Utils

A useful library of utilities for node.js used by KeystoneJS and you!

It provides additional functionality for manipulating and converting various types of data, including converting strings between various forms, and lightweight html ← → text conversion.

Usage

npm install keystone-utils --save

... then ...

var utils = require('keystone-utils');
console.log(utils.isObject({})); // true!

Bundled with KeystoneJS

If you're using KeystoneJS, it exposes this library as .utils.

var keystone = require('keystone');
var utils = keystone.utils;

Test utilities

  • isFunction(arg) - determines if arg is a function
  • isObject(arg) - determines if arg is an object
  • isValidObjectId(arg) - determines if arg looks like a valid MongoDB ObjectId
  • isArray(arg) - determines if arg is an array
  • isDate(arg) - determines if arg is a date
  • isString(arg) - determines if arg is a string
  • isNumber(arg) - determines if arg is a number
  • isDataURL(arg) - determines if arg is a base64 encoded data URI
  • isEmail(arg) - make sure arg looks like a valid email address
    • Uses a regular expression to check, so may cause false-negatives in extremely rare cases. See http://www.regular-expressions.info/email.html

Option utilities

  • options(defaults, options) - copies and merges options into the defaults
  • optionsMap(arr, property, clone) - creates a map of options
    • Turns an array of objects into an object of objects, with each object under the value of property
    • Performs a deep clone of the objects when clone is set to true

Function utilities

  • noop() - a simple function that does nothing ("no operation")
  • defer(fn, args...) - wraps the function and invokes it in process.nextTick, great for Zalgo containment
  • bindMethods(obj, scope) - recursively binds method properties of obj to scope and returns a new object containing the bound methods.

Random utilities

  • randomString(len, chars) - Generates a 'random' string of characters to the specified length (uses Math.random).
    • len can be an array of [min, max] length to generate
    • chars is a string of characters to include, defaults to 0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz

Conversion utilities

  • number(arg) - converts a string to a number, accepting human-friendly input
    • e.g. 1,432 » 1432, $1432 » 1432, 2.5 » 2.5
  • escapeRegExp(str) - escapes a string to be safely converted to a regular expression
  • escapeString(str) - escapes a string to be safely used as a Javascript string literal
  • stripDiacritics(str) - stips accented characters from a string, replacing them with their simple equivalents
  • transliterate(str) - transliterates Russian and Ukrainian words from cyrillic to latin
  • singular(str) - converts a string to its singular form
  • plural(count, singular, plural) - displays the singular or plural of a string based on a number or number of items in an array.
    • Replaces * in the string with the number
    • Will automatically convert singular to plural when plural is not provided
    • When only given one argument, will return the plural form of a string
    • e.g.
      • plural(1, '* thing') » '1 thing'
      • plural(2, '* thing') » '2 things'
      • plural([1,2], 'single', 'couple') » 'couple'
      • plural('friend') » 'friends'
  • upcase(str) - converts the first letter in a string to Uppercase
  • downcase(str) - converts the first letter in a string to lowercase
  • titlecase(str) - converts a string to Title Case
  • camelcase(str, lowercase) - converts a string to camelCase
    • The lowercase argument causes the first letter to be lowercase, and default to true.
  • decodeHTMLEntities(str) - decodes html entities in a string
  • encodeHTMLEntities(str) - encodes html entities in a string
  • stringify(obj) - safely stringifies an object to JSON for output in JavaScript source (escapes illegal JS but valid JSON unicode characters)
  • textToHTML(str) - lightweight conversion of text to HTML (line breaks to <br>)
  • htmlToText(str) - lightweight conversion to HTML to text
    • Really only useful when you need a lightweight way to remove html from a string before cropping it, so you don't end up with partial tags or an invalid DOM structure.
    • It will convert br, p, div, li, td, th tags to single line-breaks. All other tags are stripped.
    • Multiple line breaks are then compressed to a single line break, and leading / trailing white space is stripped.
    • For a more sophisticated use-case, you should check out the to-markdown and html-to-text packages on npm.
  • cropString(str, length, append, preserveWords) - crops a string to the specified length
    • You can optionally provide a string to append (only appended if the original string was longer than the specified length).
    • If preserveWords is true, the length is extended to the end of the last word that would have been cropped.
  • cropHTMLString(str, length, append, preserveWords) - crops an HTML string safely by converting it to text, cropping it, then converting it back to HTML
  • slug(str, separator) - generates a slug from a string. Word breaks are hyphenated.
    • separator defaults to '-'
  • keyToLabel(str) - converts a key to a label
    • e.g. keyToLabel('myKey') » My Key
  • keyToPath(str, plural) - converts a key to a path
    • Like slug(keyToLabel(str)) but will optionally converts the last word to a plural.
    • e.g. keyToPath('someThing', true) » some-things
  • keyToProperty(str, plural) - Converts a key to a property.
    • Like keyToPath but converts to headlessCamelCase instead of dash-separated
  • calculateDistance(point1 [lat, lng], point2 [lat, lng]) - Returns the distance between two [lat,lng] points in radians
  • kmBetween - Returns the distance between two [lat,lng] points in kilometers
  • milesBetween - Returns the distance between two [lat,lng] points in miles

Credits

Uses the inflect library for singular / plural conversion, see https://github.com/pksunkara/inflect or npm info i

Some utils are borrowed from / inspired by mongoose/utils.js, see https://github.com/LearnBoost/mongoose

HTML Entity encode / decode is based on code in node-html-to-text, see https://github.com/werk85/node-html-to-text

The transliteration code is based on https://www.npmjs.org/package/transliteration.cyr