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

util.settings

v0.0.5

Published

Settings management singleton for a web application.

Downloads

9

Readme

util.settings

Settings management singleton for a web application

build analysis code style: prettier testing NPM

Installation

This module uses yarn to manage dependencies and run scripts for development.

To install as an application dependency:

$ yarn add util.settings

To build the production version of the app and run all tests:

$ yarn run all

This module uses puppeteer. If working in Linux, see this troubleshooting guide for setting up "Chrome Linux Sandbox". The automated test cases will start an express server, use puppeteer to load the page, run some confirming tests, and then shutdown the server once complete. Manual testing can also be performed after building the application by starting the server:

$ yarn start

This will start a localhost server on port 4000. To stop the server use:

$ yarn stop

Also note when building this module one may see the warning "npm WARN lifecycle The node binary used for scripts is ...". To get rid of this warning in the build environment add the following to ~/.npmrc:

scripts-prepend-node-path=true

This can also be done via the CLI with:

$ npm config set scripts-prepend-node-path true

Overview

This module contains code to manage and persist settings for a web application using localforage. A section is registered with the settings instance singleton using the register function. These settings are then placed into a simple object that is monitored by a Proxy using the on-change library. When the application makes changes to this object the changes are automatically persisted to local storage using the localforage package.

Usage

To retrieve the Settings object instance and register new configuration options, use the .instance() Promise factory:

import {SectionConfig, Settings} from "util.settings";

Settings.instance()
    .then((instance: Settings) => {

        const newSettings: SectionConfig = {
		    name: "general",
		    default: {
			    key1: "defaultValue1",
			    key2: "defaultValue2",
			    key3: "defaultValue3"
            }
        };

        instance.register(newSettings);
        window.settings = instance.root;
    })
    .then(() => {
        // initialize the application
        // use settings like:
        //     `window.settings.general.key1`
        //     `window.settings["general"]["key1"]
    });

The instance() call is a thenable. When the promise instance is exectued it is initialized and passed to then. During this initialization previous settings are retrieved from localforage and saved in the root settings object. Inside of then the register() is called to add a section to the settings. The example above adds a new section named general with three settings keys. If the section already exists it is used instead of adding a new section.

The settings object is then retrieved and set globally on the window via a call to instance.root. Each of the setting can then be retrieved like any normal Javascript object in window.settings, e.g. window.settings.general.key1. Changes that are made to this settings object are captured via a Proxy and automatically persisted to the localforage. The following data types can be used:

  • Array
  • ArrayBuffer
  • Blob
  • Float32Array
  • Float64Array
  • Int8Array
  • Int16Array
  • Int32Array
  • Number
  • Object
  • Uint8Array
  • Uint8ClampedArray
  • Uint16Array
  • Uint32Array
  • String

Note that there is an issue with keys that contain complex objects (e.g. An array of strings associated to a key). If the underlying object is manipulated, then the Proxy will not properly invoke the associated save. This can be worked around by cloning the underlying object, manipulating the clone, and then assigning the clone back to the key.

An idea to fix this was to use ImmutableJS, but that will not work in this library because this object type is not properly serialized by localforage.

API

functions

properties

  • .noProxyRoot - a read only version of the settings object without the proxy overlay. Changes to this object will NOT persist.
  • .root - a reference to the settings object with proxy overlay. This should be used to interact with all settings.
  • .sections - an array of all setions contained within the settings object.