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

schema-settings

v1.0.0

Published

JSON Schema based settings provider

Downloads

4

Readme

schema-settings

Build Status

JSON Schema based settings provider :gear:

const SSettings = require('schema-settings')
const settings = new SSettings({
  schema: {
    properties: {
      foo: { type: 'string', default: 'bar' }
    }
  },
  location: `${__dirname}/settings.json`
})


const foo = settings.get('foo')

// prints 'bar'
console.log(foo)

I wanted a strongly constrained settings implementation that made it easy for folks to define additional setting constraints (as external plugins). I also wanted it to be easy to use. I think schema-settings does those things well.

API

First, install with npm i schema-settings

global

A global SchemaSettings instance, for use when you don't wish to maintain an instance.

This is designed for electron renderer process workloads, but may be broadly useful. Simply configure once and then reference where needed. For example:

const settings = require('schema-settings`).global
settings.configure({
  schema: {
    properties: {
      todo: {type: 'string', default: 'populate this json schema constraints object' }
    }
  },
  location: './path-on-disk-to-save-settings-to.json'
})

// 'populate this json schema constraints object'
settings.get('todo')

SchemaSettings

constructor

/**
* Creates an instance of SchemaSettings
* 
* By default, this uses coercion and defaults for the provided schema
* 
* @param ISchemaSettingsArgs Ctor args
*/

Arguments:

/**
* The ajv compatible JSONSchema definition
*/
schema : object,

/**
* The location of the settings data file
*/
location : string,

/**
* A readFile implementation
*/
readFile ?: readFileSync,

/**
* A writeFile implementation
*/
writeFile ?: writeFileSync,

/**
* The ajv compilation arguments
*/
ajvArgs ?: Options,

/**
* The separator used to denote object hierarchy in selectors
*/
hierarchySeparator ?: string,

/**
* The separator used to denote the start of array indices in selectors
*/
arrayStartSeparator ?: string,

/**
* The separator used to denote the end of array indices in selectors
*/
arrayEndSeparator ?: string

Example:

const SS = require('schema-settings`)
const settings = new SS({
  schema: {
    properties: {
      todo: {type: 'string', default: 'populate this json schema constraints object' }
    }
  },
  location: './path-on-disk-to-save-settings-to.json'
})

get

/**
* Get's all or a portion of the settings
* @param selector sub object selector
*/

Arguments:

/**
* sub object selector used to determine what portion of the settings object you demand
* Optional
*/
selector ?: string

Example:

const todo = settings.get('todo')

// outputs 'populate this json schema constraints object'
console.log(todo)

getTree

Designed for internal use only

/**
* Get's a portion of settings, maintaining the object tree leading to that portion
* @param selector sub object selector
*/

Arguments:

/**
* sub object selector used to determine what portion of the settings object you demand
* Optional
*/
selector ?: string

Example:

const todoTree = settings.getTree('todo')

// outputs {todo: 'populate this json schema constraints object'}
console.log(todoTree)

set

/**
* Set's a portion of the settings
* @param selector sub object selector
* @param val value to store
*/

Arguments:

/**
* sub object selector used to determine what portion of the settings object you wish to change
* Optional
*/
selector ?: string

/**
* The value you wish to set for settings
* Note: If you omit the selector argument, this argument will be first
*/
val : object

Example:

// will write a new value for 'todo'
settings.set('todo', 'i\'ve done it now!')

walkTree

Designed for internal use only

/**
* Walks toward a selector against a given object and calls callback for each step
* @param selector sub object selector
* @param obj object
* @param cb step callback, given (obj ptr, selector part, selector part index, total selector parts count)
*/

Arguments:

/**
* sub object selector used to determine what portion of the settings object you wish to walk toward
*/
selector : string

/**
* The object you wish to walk over
*/
obj : object

/**
* The callback that will be invoked for each step
* it's given:
*    obj: a pointer to the node in the object we are currently walking on
*    key: the key that we are about to walk into
*    index: the index of the key, as a part of the overall selector
*    keyCount: the number of total keys that makeup the overall selector
*/
cb : (obj : object, key : string, index : number, keyCount : number)

evaluateSelector

/**
* Evaluates a selector against a given object and returns the selected bits
* @param selector sub object selector
* @param obj object
* @param type the evaluation type we use (copy-tree, or reference src object)
*/

Arguments:

/**
* sub object selector used to determine what portion of the settings object you wish to walk toward
*/
selector : string

/**
* The object you wish to walk over
*/
obj : object

/**
* The evaluation type we use ('reference' or 'copy')
* This determines if we reference a part of the larger object in the return type
* or if we copy the object/relevant bits to it's own structure
*/
type : 'reference' | 'copy' = 'reference'

generateSelectors

/**
* Generates selectors for a given object shape
* @param srcObject object for which to generate selectors
*/

Arguments:

/**
* The object you wish to generate selectors for
*/
srcObject : object

Example:

const selectors = settings.generateSelectors({todo: 'hello'})

// outputs ['todo']
console.log(selectors)

License

MIT