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

figson

v0.0.7

Published

Simple config file storage.

Downloads

5

Readme

Figson

Build Status NPM version Dependency Status devDependency Status Coverage Status

Simple configuration storage.

Note: This project is in early development, and versioning is a little different. Read this for more details.

Why should you care?

This project is in very early development, but already makes working with JSON, CSON and YAML configuration files a lot easier.

Installation

$ npm install figson --save

Usage

Async Example

var figson = require('figson');
var path   = require('path');

figson.parse('./config.json', function(error, config) {
  if (error) { throw error; }
  config.set('foo', 'bar');
  config.save(function(error) {
    if (error) { throw error; }
  });
});

Sync Example

var figson = require('figson');
var path   = require('path');

try {
  var config = figson.parse('./config.json');
  config.set('foo', 'bar');
  config.save();
} catch (error) {
  throw error;
}

You can swap out config.json for config.cson, config.yaml or config.yml. Everything will still work, and uses the exact same API as described below.

Figson API

Figson itself exposes one method:

figson.parse(config_file, [callback]);

Asynchronously reads a configuration file (if there is a callback function), parses it, and exposes an error and a config object to the callback (function(error, config) {}). config_file is the path to the file. Omit the callback for a synchronous operation.

Config API

The config object is basically just a tiny wrapper around the data inside the configuration file. It exposes a few properties and methods. All of config's methods are chainable, and accessing a property with a config method uses a tiny DSL string similar to how you would access that property using JavaScript's dot notation.

config.data

An object representing the configuration file.

config.val()

Returns the value of the last key/property in the config chain.

config.get([key])

Retrieves the value of the given key. If no key is provided, it retrieves the value of the last known key in the chain.

key is a string containing the object property who's value you want to retrieve.

Example:

// { some: { property: 'value1' }}
config.get('some.property').val() // => value1
// or
config.get().val() // => value1

config.set([key], value)

Sets the key to the value. If no key is given, uses the most recent key in the chain. The value can be a string, number, object, array or null.

Example:

// { an: { array: { property: [] }}}
config.set('an.array.property[0]', 'the value') // { an: { array: { property: ['the value'] }}}
// or
config.set('a different value') // { an: { array: { property: ['a different value'] }}}

config.update([key], value)

First, attempts a get() to determine the existence of the given key property. If it exists, it will then call set() with the new value. Otherwise, throws an error. Useful if you need to safely set a value. If no key is given, updates the key from the last call in the chain.

Example:

config.update('foo', 'baz')
config.get('foo').update('baz')

config.destroy([key])

"deletes" the given key property by setting it's value to undefined. If no key is given, it will "delete" the key from the last method in the chain.

Example:

config.destroy('foo');
config.get('bar').destroy()

config.find(partial_key)

This method is useful for accessing properties that are very deeply nested. Suppose you have an object:

{
  a: {
    very: {
      deeply: {
        nested: {
          property: 'value'
        }
      }
    }
  }
}

You want to update a.very.deeply.nested.property to have the value foobar, but you don't want to have to type out the whole property name. Just use .find()! As long as the property ends with the key you pass to find(), it'll work:

config.find('nested.property').set('foobar') // done!

config.save([callback])

This saves the current state of config.data to the configuration file. This is a synchronous operation, but passing in an optional callback (function(error) {}) will make it perform asynchronously.

Example:

config.save(); // synchronous

// asynchronous
config.save(function(error) {
  if (error) { throw error; }
});

Adding your own configuration file types

Figson exposes a small interface for you to add your own configuration file handlers, if you wish to use a separate data format. Figson uses this interface internally to add support for JSON, CSON and YAML files.

All you have to do is call figson.addHandler(name, object), where name is the name of your handler (e.g. "CSON" for CSON files) and where object is a JavaScript object that lists file extensions as well as synchronous and asynchronous parse and stringify operations. For example, if you wanted to register JSON, you would do this:

figson.addHandler('JSON', {
  extensions: ['.json'],
  parse: JSON.parse,
  parseSync: JSON.parse,
  stringify: JSON.stringify,
  stringifySync: JSON.stringify
});

Figson will automatically register the file extension to the correct data format. You can look in lib/handlers for other examples.

Contributing:

Please read the contribution guidelines.