Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imenesesl/scheme-adapter
scheme-adapter
https://github.com/imenesesl/scheme-adapter
Last synced: 3 days ago
JSON representation
scheme-adapter
- Host: GitHub
- URL: https://github.com/imenesesl/scheme-adapter
- Owner: imenesesl
- Created: 2021-02-12T15:27:13.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-02-13T20:33:23.000Z (almost 4 years ago)
- Last Synced: 2024-08-08T15:18:20.646Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 34.2 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# scheme-adapter
#### `Super lightweight package to manage your models with schemes`
- ## Scheme - adapter
```ts
export declare type Scheme = {
[P in keyof Required]: M[P];
};export declare const adapter: (item: any, schema: Scheme) => M;
```Set your default values using a base interface
_The schema requires you to define all the properties_
#### Example
```ts
import { adapter, Scheme } from 'scheme-adapter';export interface User {
name: string;
email: string;
isActive: boolean;
}export const UserScheme: Scheme = {
email: '[email protected]',
isActive: false,
name: '',
};export const userAdapter = (user: any): User => adapter(user, UserScheme);
// in
userAdapter({ name: 'Luis Meneses' });// out
{
email: '[email protected]',
isActive: false,
name: 'Luis Meneses',
}
```- ## SchemeTransform - transformAdapter
```ts
export declare type Operators = '' | ' ' | 'and' | 'or' | 'join-array' | 'join-map' | 'sum' | 'rest' | 'divide' | 'multiply';interface TransformPayload {
value: M[P];
transforms: Array;
join?: Operators;
}export declare type SchemeTransform = {
[P in keyof Required]: TransformPayload;
};export declare const transformAdapter: (item: any, schema: SchemeTransform) => M;
```Define their default values using a base interface and the different properties in a standard model
Each property provide three options by TransformPayload where:
- `value`: Default value
- `transforms`: List of properties to join
- `join`: Operator to join properties⚠️ Warning
In this version the `join` property only works to operations with booleans, numbers, arrays, maps and strings, if you need more operations, it is suggested that you perform these operations separately.
_The schema requires you to define all the properties_
#### Example #1 - strings
```ts
import { SchemeTransform, transformAdapter } from 'scheme-adapter';export interface User {
name: string;
email: string;
isActive: boolean;
}export const UserSchemaTransfrom: SchemeTransform = {
email: {
value: '',
transforms: ['personalEmail'],
},
isActive: {
transforms: ['status'],
value: false,
},
name: {
transforms: ['name', 'lastName'],
join: ' ',
value: '',
},
};export const userTransform = (user: any): User => transformAdapter(user, UserSchemaTransfrom);
// in
userTransform({
personalEmail: '[email protected]',
name: 'Luis',
lastName: 'Meneses',
});// out
{
email: '[email protected]',
isActive: false,
name: 'Luis Meneses',
}
```#### Example #2 - numbers
```ts
import { SchemeTransform, transformAdapter } from 'scheme-adapter';export interface MathOperations {
sum: number;
rest: number;
divided: number;
multiply: number;
}export const MathOperationsSchemeTranform: SchemeTransform = {
divided: {
transforms: ['DaysPerYear', 'MonthsPerYear', 'HoursPerDay'],
join: 'divide',
value: 0,
},rest: {
transforms: ['CurrentYear', 'BirthDate'],
join: 'rest',
value: 0,
},
sum: {
transforms: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
join: 'sum',
value: 0,
},
multiply: {
transforms: ['DaysPerYear', 'HoursPerDay'],
join: 'multiply',
value: 0,
},
};export const mathOperationsTransform = (mathOperations: any): MathOperations =>
transformAdapter(mathOperations, MathOperationsSchemeTranform);// in
mathOperationsTransform({
January: 31,
February: 28,
March: 31,
April: 30,
May: 31,
June: 30,
July: 31,
August: 31,
September: 30,
October: 31,
November: 30,
December: 31,
CurrentYear: 2021,
BirthDate: 1995,
DaysPerYear: 365,
MonthsPerYear: 12,
HoursPerDay: 24,
});// out
{
divided: 1.2673611111111112,
rest: 26,
sum: 365,
multiply: 8760,
}
```#### Example #3 - booleans
```ts
import { SchemeTransform, transformAdapter } from 'scheme-adapter';export interface BooleanOperations {
isPremium: boolean;
isFreePremium: boolean;
}export const BooleanOperationsSchemeTranform: SchemeTransform = {
isFreePremium: {
transforms: ['isActive', 'isCertificated'],
join: 'or',
value: false,
},
isPremium: {
transforms: ['isActive', 'isPremium', 'isCertificated'],
join: 'and',
value: false,
},
};export const booleanOperationsTransform = (booleanOperations: any): BooleanOperations =>
transformAdapter(booleanOperations, BooleanOperationsSchemeTranform);// in
booleanOperationsTransform({
isActive: true,
isPremium: false,
isCertificated: true,
});// out
{
isFreePremium: true,
isPremium: false,
}
```#### Example #3 - maps
```ts
import { SchemeTransform, transformAdapter } from 'scheme-adapter';export interface MapOperations {
user: User;
contact: Contact;
owner: User;
}export const MapOperationsSchemeTranform: SchemeTransform = {
contact: {
transforms: ['location', 'contact'],
join: 'join-map',
value: {
country: '',
phone: '',
},
},
user: {
transforms: ['status', 'user', 'contact'],
join: 'join-map',
value: {
email: '',
isActive: false,
name: '',
},
},
owner: {
transforms: ['provider'],
join: 'join-map',
value: {
email: '',
isActive: false,
name: '',
},
},
};export const mapOperationsTransform = (mapOperations: any): MapOperations =>
transformAdapter(mapOperations, MapOperationsSchemeTranform);// in
mapOperationsTransform({
location: {
country: 'Colombia',
code: '+57',
},
contact: {
phone: '0000000000',
email: '[email protected]',
},
status: {
isActive: true,
},
user: {
name: 'Luis',
lastName: 'Meneses',
},
provider: {
email: '[email protected]',
isActive: true,
name: 'ORG',
},
});// out
{
contact: { country: 'Colombia', phone: '0000000000' },
user: { email: '[email protected]', isActive: true, name: 'Luis' },
owner: { email: '[email protected]', isActive: true, name: 'ORG' },
}
```#### Example #3 - arrays
```ts
import { SchemeTransform, transformAdapter } from 'scheme-adapter';export interface ArrayOperations {
users: Array;
}export const ArrayOperationsSchemeTranform: SchemeTransform = {
users: {
transforms: ['premium', 'free', 'freemium'],
join: 'join-array',
value: [],
},
};export const arrayOperationsTransform = (arrayOperations: any): ArrayOperations =>
transformAdapter(arrayOperations, ArrayOperationsSchemeTranform);// in
arrayOperationsTransform({
premium: Array.from(Array(10).keys()).map(user => userAdapter(user)),
free: Array.from(Array(10).keys()).map(user => userAdapter(user)),
freemium: Array.from(Array(10).keys()).map(user => userAdapter(user)),
});// out
{
users: [ {...1}, {...2}, ..., {...30} ],
}
```## Get Started
- Add the following in the dependencies of your package.json
```bash
npm install scheme-adapter
```- Ready to use
```ts
import { adapter, Scheme, SchemeTransform, transformAdapter } from 'scheme-adapter';
```