0.10.0 • Published 10 months ago

ts-data-class v0.10.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

TS Data-Class

codecov

This package aims to simplify and secure data parsing and manipulation in typescript by providing auto-generated constructors, helper methods, and parsing utilities with minimal boilerplate and full intellisense support.

Every instance of the data class will automatically have:

  • A typed constructor
  • copy & copyDeep
  • parse & tryParse
  • equals

Installation

npm: npm install ts-data-class

yarn: yarn add ts-data-class

Usage

Setup

  1. Define a class T that extends DTClass<T>
  2. Define class members, marking optional with ?: and required with !:
  3. Define the parsers getter with a parser for each of the fields (full intellisense support)
  4. Use the class as you would any other but with all the data class perks and full intellisense support!
Example:
class Owner extends DTClass<Owner> {
  name!: string;
  age?: number;

  protected get parsers(): DTParsers<Owner> {
    return {
      name: (v) => Parsers.string(v) ?? "unknown name",
      age: (v) => Parsers.number(v) ?? -1,
    };
  }
}

class Cat extends DTClass<Cat> {
  numLives!: number; // required
  breed!: string; // required
  name?: string; // optional
  owner!: Owner; // required

  protected get parsers(): DTParsers<Cat> {
    return {
      numLives: (v) => (typeof v === "number" ? v : 9),
      breed: (v) => (typeof v === "string" ? v : "stray"),
      name: (v) => (typeof v === "string" ? v : undefined),
      owner: (v) => Owner.tryParse(v) ?? Owner.empty(),
    };
  } // required
}

const cat1 = new Cat({
  numLives: 8,
  name: "Whiskers",
  breed: "Bald",
  owner: new Owner({ name: "jack", age: 2 }),
});
console.log(cat1);
/**
  Cat {
    numLives: 8,
    breed: 'Bald',
    name: 'Whiskers',
    owner: Owner { name: 'jack', age: 2 }
  }
 */

const cat2 = cat1.copy({
  name: "Kitteh",
  breed: undefined,
  owner: undefined,
});
console.log(cat2);
/**
  Cat {
    numLives: 8,
    breed: 'stray',
    name: 'Kitteh',
    owner: Owner { name: 'unknown name', age: -1 }
  }
*/

const cat3 = cat2.copy({
  breed: "tiger",
  owner: cat2.owner.copy({
    name: "bobby",
  }),
});
console.log(cat3);
/**
  Cat {
    numLives: 8,
    breed: 'tiger',
    name: 'Kitteh',
    owner: Owner { name: 'bobby', age: -1 }
  }
 */
0.10.0

10 months ago

0.9.1

1 year ago

0.8.9

2 years ago

0.9.0

2 years ago

0.8.8

2 years ago

0.8.5

2 years ago

0.8.4

2 years ago

0.8.7

2 years ago

0.8.6

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.8.3

2 years ago

0.7.13

2 years ago

0.7.12

2 years ago

0.7.11

3 years ago

0.6.71

3 years ago

0.7.1

3 years ago

0.6.7

3 years ago

0.6.6

3 years ago

0.6.8

3 years ago

0.7.0

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.6.3

3 years ago

0.6.21

3 years ago

0.6.2

3 years ago

0.6.11

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.72

3 years ago

0.2.71

3 years ago

0.2.70

3 years ago

0.2.69

3 years ago

0.2.68

3 years ago

0.2.67

3 years ago

0.2.66

3 years ago

0.2.65

3 years ago

0.2.64

3 years ago

0.2.63

3 years ago

0.2.62

3 years ago

0.2.61

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago