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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@rnacanvas/utilities

v6.5.0

Published

General utilities

Readme

Installation

With npm:

npm install @rnacanvas/utilities

Usage

All exports of this package can be accessed as named imports.

// some example imports
import { detectMacOS } from '@rnacanvas/utilities';
import { DownloadableFile } from '@rnacanvas/utilities';
import { KeyBinding } from '@rnacanvas/utilities';

function first()

Returns the first item in an array.

Throws for empty arrays.

first([5, 4, 3, 2, 1]); // 5

first(['b']); // "b"

first([]); // throws

function last()

Returns the last item in an array.

Throws for empty arrays.

last([5, 4, 3, 2, 1]); // 1

last(['b']); // "b"

last([]); // throws

function middleThree()

Returns the middle three items in an array.

Throws for arrays with less than three items and arrays with an even number of items.

middleThree([1, 2, 3, 4, 5]); // [2, 3, 4]

middleThree([1, 2, 3, 4, 5, 6]); // throws

middleThree([]); // throws
middleThree([1]); // throws
middleThree([1, 2]); // throws
middleThree([1, 2, 3]); // [1, 2, 3]

function splitLines()

Splits a string into its constituent lines regardless of newline character encoding (e.g., CR, LF, CRLF).

splitLines('asdf\nqwer'); // ["asdf", "qwer"]

splitLines('1\n2\r3\r\n4'); // ["1", "2", "3", "4"]

Note that empty strings might be present in the returned array after splitting.

splitLines('\nasdf'); // ["", "asdf"]

splitLines('qwer\r'); // ["qwer", ""]

splitLines('asdf\n\n\nqwer'); // ["asdf", "", "", "qwer"]

function isJSONSerializable()

Return true if the provided value can be serialized using the JSON.stringify() method without the JSON.stringify() method throwing.

Returns false otherwise.

var obj1 = { 'a': 1, 'b': 2 };

isJSONSerializable(obj1); // true

var obj2 = {};

// a circular reference
obj2.obj2 = obj2;

isJSONSerializable(obj2); // false

function shuffled()

Returns a new array containing the same values as in the input array but shuffled in random order.

(Does not modify the input array of values.)

shuffled([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4]

function hasFocus()

The hasFocus() function returns true if and only if the passed in node is the active element of the document.

hasFocus(document.activeElement); // true

hasFocus(document.createElement('div')); // false

function containsFocus()

The containsFocus() function returns true if and only if the passed in node contains the active element of the document (or is the active element of the document).

var ele1 = document.createElement('div');
var ele2 = document.createElement('div');

// element 1 contains element 2
ele1.append(ele2);

// make elements focusable
ele1.tabIndex = 0;
ele2.tabIndex = 0;

ele1.focus();
containsFocus(ele1); // true
containsFocus(ele2); // false

ele2.focus();
containsFocus(ele1); // true
containsFocus(ele2); // true

class DownloadableFile

Represents a file that can be downloaded by the user.

var file = new DownloadableFile('A file.txt', 'Some text.', { type: 'text/plain' });

file.name; // "A file.txt"
file.content; // "Some text."
file.type; // "text/plain"

// download the file for the user
file.download();

A blob can be used in place of a string for the content of a downloadable file.

var blob = new Blob(['<p>Hello</p>'], { type: 'text/html' });

var file1 = new DownloadableFile('hello.html', blob);

// specifying file type overrides blob type
var file2 = new DownloadableFile('hello.txt', blob, { type: 'text/plain' });

file2.type; // "text/plain"
file1.type; // "text/html"

All constructor parameters are optional.

var file = new DownloadableFile();

// default values
file.name; // "Unnamed.txt"
file.content; // ""
file.type; // "text/plain"

class KeyBinding

The KeyBinding class represents a key binding.

See the @rnacanvas/key-bindings package for documentation.

function detectWindows()

Returns true if it is detected that the user is using Windows and returns false otherwise.

detectWindows();

function detectMacOS()

Returns true if it is detected that the user is using macOS and returns false otherwise.

detectMacOS();