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

cross-js-base

v2.1.0

Published

A collection of hand-rolled JavaScript scripts the author though would be useful to package for reuse in his projects.

Downloads

23

Readme

cross-js-base (2018)

This project includes some very generic tools for front-end use that I wanted to write myself. I'm not trying to replace Lodash or other battle-tested libraries. I just sometimes write vanilla JS like this for practice. Everything is Babel-transpiled into "browsers": "> 0.25%, not dead" and webpacked.

Retrospect in 2023

I hardly remember my methods and motives of 2018, but I think in this project I just intended to collect various scripts common to my projects. I'm fairly certain I thought I would end up putting more scripts in this than what you see.

I have updated this package (as of 2023) so that it builds and all tests pass. It is still very much JavaScript written in the style of its time, circa 2018, and transpiled in the style of the day. I have not attempted to modernize the scripts. I simply updated the dependencies so you can install it in a dev environment. I'm working on a Windows 11 machine in June 2023 with Node v18.16.0. I'm installing using npm install. As of this moment, it installs correctly with 0 vulnerabilities and no warnings about deprecated dependencies. My goal for this update was just to make this package something I'm slightly less embarrassed of, but that does not mean this is code like what I would write today.

Installation

There's an npm package, so you can install it as

npm install cross-js-base

or

yarn add cross-js-base

Then to use the individual tools import them by writing, for example

import { StoreLocal } from "cross-js-base"

Testing

Testing is written using Jest. After doing a full yarn install run

yarn run test

Tools

StoreLocal

import { StoreLocal } from "cross-js-base"

StoreLocal is a small convenience wrapper around the browser localStorage object.

let localStore = StoreLocal.build("my-unique-index");
// for obvious reasons, the build method does not
// overwrite the existing content defined at the key
// "my-unique-index"; it creates a new data structure 
// if necessary.  The value of assigned to 
// "my-unique-index" in localStorage should be a 
// JSON-formated array or null.  That is: it should be a 
// string that JSON.parse can parse into a JavaScript
// array or it should be null.   If the existing 
// data is anything else, the build method will throw
// an error.
localStore.addItem("apple", "1");
// for most predictable results, both key and value
// should be strings because that is how browser
// local storage works.  This tool will not 
// fail elegantly if you use it incorrectly
localStore.addItem("pear", "2");
localStore.getItem("apple"); // returns "1"
localStore.getItem("bear"); // on unknown key returns null
localStore.getIndex(); // returns array: ["apple", "pear"]
// the index is not guaranteed to be returned
// in any particular order
localStore.removeItem("apple");
localStore.getIndex(); // returns array: ["pear"]
localStore.removeItem("apple"); // does nothing; no error
localStore.addItem("pear", "5"); // no error, just updates value
// addItem is the way to update the value associated 
// to a key; there is no separate method for that

StringHash

import {StringHash} from "cross-js-base"

let hash = StringHash.hash("apple");