Git Product home page Git Product logo

haf's Introduction

๐Ÿง  ๐Ÿ”’ Haf ๐Ÿฆบ โœ๏ธ

npm version CI Known Vulnerabilities Maintainability Test Coverage

Haf is a fully typed ๐Ÿ”’, cross-platform, persistent ๐Ÿ’พ JSON storage โš™๏ธ solution for your NodeJS projects with a great developer experience!

  • โœ๏ธ Auto-completed dot-notation suggestions as you type when you try to get()/set()/delete()/reset() data from the store.
  • โœ… The type of the value you get() from the store is correctly inferred. So you always know what you'll get().
  • โŒ Non-nullable values aren't suggested on delete(). Trying to delete() a non-nullable field will throw a type error.

Go to gifs section to see it in action!

Installation

npm i @batuhanw/haf

OR

yarn add @batuhanw/haf

๐Ÿƒ Getting Started

1. Define Your Schema

interface DogSchema {
  name: string;
  age: number;
  toys: string[];
  vaccines: { name: string; date: string; nextDate?: string }[];
  appearance: {
    eyeColor: string;
    hairColor: {
      primary: string;
      secondary?: string;
      otherColors: string[];
    };
  };
  sterilizedAt?: string;
  hasPuppies: boolean;
}

2. Initiate Haf

import Haf from '@batuhanw/haf';

const haf = new Haf<DogSchema>({
  name: 'myDog',
  defaultSchema: {
    name: 'Pop',
    age: 2,
    toys: ['socks', 'toilet paper'],
    vaccines: [
      { name: 'rabbies', date: '2020-01-01' },
      { name: 'parasite', date: '2020-01-01', nextDate: '2020-01-03' },
    ],
    appearance: {
      eyeColor: 'brown',
      hairColor: {
        primary: 'white',
        secondary: undefined,
        otherColors: ['black'],
      },
    },
    sterilizedAt: undefined,
    hasPuppies: false,
  },
});

3. Enjoy

Get

  const name = haf.get('name') // string
  const age = haf.get('age') // number
  const hasPuppies = haf.get('hasPuppies') // boolean
  const vaccines = haf.get('vaccines') // { name: string; date: string; nextDate?: string }[]
  const hairColor: haf.get('appearance.haircolor') // { primary: string; secondary?: string, otherColors: string[] }
  const secondaryHairColor: haf.get('appearance.hairColor.secondary') // string | undefined

  const invalid = haf.get('non-existent') // type error

Set

haf.set('name', 'Pop');
haf.set('appearance.hairColor', { primary: 'white' });
haf.set('appearance.hairColor.secondary', 'brown');
haf.set('appearance.hairColor.secondary', undefined);
haf.set('appearance.hairColor.otherColors', ['black']); // This will overwrite existing array

haf.set('name', 1); // type error
haf.set('toys', [1, 2]); // type error
haf.set('appearance.haircolor', { primary: 1 }); //type error
haf.set('appearance.hairColor.primary', 1); // type error
haf.set('appearance.haircolor', { notExist: 'white' }); //type error

Append

Appends given values to the existing array

haf.get('toys'); // ['socks', 'toilet paper']

haf.append('toys', 'human hand', 'bone');

haf.get('toys'); // ['socks', 'toilet paper', 'human hand', 'bone']

Delete

haf.delete('sterilizedAt');
haf.delete('appearance.hairColor.secondary');

haf.delete('name'); // type error
haf.delete('appearance.hairColor.primary'); // type error

Gifs

Get

Set

Delete

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.