@transcend-io/persisted-state

1.0.4 • Public • Published

Table of Contents

Overview

This package contains a class that makes it easy to read/write JSON data from a file on disk. It's built using io-ts to provide runtime and static type validation.

Example

// A sample io-ts validation
const TestCodec = t.type({
  timestamp: t.string,
  time: t.number,
  type: t.type({
    fish: t.partial({
      dog: t.string,
    }),
  }),
  content: t.string,
});

// Create a new state before each test
const testFile = join(__dirname, 'test.json');
const state = new PersistedState(testFile, TestCodec, {
  timestamp: '',
  time: 2,
  type: {
    fish: {
      dog: 'dog',
    },
  },
  content: '',
});

// get values
const twoDeep = state.getValue('type', 'fish'); // { dog: 'dog' }
const threeDeep = state.getValue('type', 'fish', 'dog'); // 'dog'

// set values
state.setValue(3, 'time');
state.setValue('howdy', 'content');
state.setValue({ dog: 'moose' }, 'type', 'fish');

// save and wipe
state.save();
state.wipe();

Typescript Build

Build this package only:

yarn run tsc
yarn run tsc --watch # Watch mode

Create a fresh build:

yarn clean && yarn run tsc

Lint

Lint the typescript files in this package:

yarn lint

Readme

Keywords

none

Package Sidebar

Install

npm i @transcend-io/persisted-state

Weekly Downloads

974

Version

1.0.4

License

MIT

Unpacked Size

75.1 kB

Total Files

19

Last publish

Collaborators

  • transcend-bot
  • michaelfarrell76