Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@transcend-io/persisted-state

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@transcend-io/persisted-state

A type-friendly class that makes it easier to read/write state from local disk.

latest
Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
1.1K
-12.7%
Maintainers
2
Weekly downloads
 
Created
Source

Table of Contents

  • Overview
  • Typescript Build
  • Lint

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

FAQs

Package last updated on 07 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts