Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/arran4/tsobjectutils

Some typescript objects I use in a couple repos
https://github.com/arran4/tsobjectutils

json-deserialization library npm npm-package ts-library ts-objects tslang tslibrary typescript

Last synced: about 1 month ago
JSON representation

Some typescript objects I use in a couple repos

Awesome Lists containing this project

README

        

# tsobjectutils
Some typescript objects I use in a couple repos.

The intended purpose is for marshalling JSON objects into objects in a sane "semi checked" way. (You can never really
be that sure.)

Happy to accept additions, suggestions, etc.

https://www.npmjs.com/package/@arran4/tsobjectutils

# Usage

Install:
```
npm install @arran4/tsobjectutils
```

Basic idealized / intended use:
```
export class User {
constructor(
props : Partial> | null = null,
public Email : string = GetStringPropOrDefault(props, "Email", ""),
public Name : string = GetStringPropOrDefault(props, "Name", ""),
public UserUID : string = GetStringPropOrDefault(props, "UserU0ID", ""),
public Created : Date | null = GetDatePropOrDefault(props, "Created", null),
public Modified : Date | null = GetDatePropOrDefault(props, "Modified", null),
) {}
}
```

By using the `props` component you both have created:
* an option on how to initialize the object
* an easy way to copy an object
* an easy way to unmarshal a deseiralized json object into an object with some assurance as to the content

# Definitions

```typescript
export function GetDateArrayPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): Date[] | R;
export function GetDateArrayPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): Date[] | R;
export function GetDateArrayPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): Date[];
export function GetDatePropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): R | Date;
export function GetDatePropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): R | Date;
export function GetDatePropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): Date;
export function GetNumberPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): R;
export function GetNumberPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): R;
export function GetNumberPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): R;
export function GetObjectArrayFunctionPropOrDefault(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: X): X;
export function GetObjectArrayFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc): X;
export function GetObjectArrayPropOrDefaultFunction(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: () => X): X;
export function GetObjectArrayPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: X): X;
export function GetObjectArrayPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): X;
export function GetObjectFunctionPropOrDefault(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: Y): Y;
export function GetObjectFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc): Y;
export function GetObjectPropOrDefaultFunction(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: () => Y): Y;
export function GetObjectPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: Y): Y;
export function GetObjectPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): Y;
export function GetStringArrayPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): string[] | R;
export function GetStringArrayPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): string[] | R;
export function GetStringArrayPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): string[];
export function GetStringPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): R;
export function GetStringPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): R;
export function GetStringPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): R;
export function GetBooleanPropOrThrow(props:any, prop:string):boolean
export function GetBooleanPropOrDefaultFunction(props:any, prop:string, defaultFunction:any):boolean
export function GetBooleanPropOrDefault(props:any, prop:string, defaultValue:boolean):boolean
export type ConstructorFunc = (params: Partial>) => Y;
```

Check [Tests for example usage.](./src/index.test.ts)