Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rijudev/parseus

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rijudev/parseus

Parseus

  • 0.0.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
118
increased by34.09%
Maintainers
2
Weekly downloads
 
Created
Source

Parseus

Parseus is a javascript library written in Typescript which allow marshall/unmarshall JSON into class instance. This library is able to run in NodeJS, Typescript or any JS platform. API Docs

Usage Javascript (ESNEXT)

class Person {
  @Field({ type: 'string', name: 'person_name' })
  name

  @Field({ readOnly: true, name: 'person_age', type: 'number' })
  age

  @Field({ name: 'person_last_name', type: 'string' })
  lastName

  @Field({ name: 'person_gender', default: 'M', type: 'string' })
  gender

  @Field({ name: 'person_created_at', type: 'date' })
  createdAt
}

const data = {
  person_name: 'Jhon',
  person_last_name: 'Smith',
  person_age: 25,
  person_created_at: '2018-01-01T12:00:00.000Z'
}
/**
 *  Returns an instance of Person like
 * {
 *   name:'Jhon',
 *   lastName: 'Smith',
 *   age: 25,
 *   gender: 'M',
 *   createdAt: Mon Jan 01 2018 08:00:00 GMT-0400 (Atlantic Standard Time) {}
 * }
 **/

const person = Parseus.decode(data).to(Person)
person.gender = 'F'
person.age = 18
person.name = 'Sara'

/**
 * The second parameter is optional, but if
 * the class instance has been mutated
 * we should pass the original class for references
 *
 * Returns an object with the next structure { [key:string]: any }
 * {
 *   "person_name": "Sara",
 *   "person_last_name": "Smith",
 *   "person_age": 18,
 *   "person_gender": F,
 *   "person_created_at": "2018-01-01T12:00:00.000Z"
 * }
 **/
const personMarshalled = Parseus.encode(person, Person)

Usage Typescript

class Person {
  @Field({ type: 'string', name: 'person_name' })
  name?: string

  @Field({ readOnly: true, name: 'person_age' })
  age?: number

  @Field({ name: 'person_last_name' })
  lastName?: string

  @Field({ name: 'person_gender', default: 'M' })
  gender?: string

  @Field({ name: 'person_created_at', type: 'date' })
  createdAt?: Date
}

const data = {
  person_name: 'Jhon',
  person_last_name: 'Smith',
  person_age: 25,
  person_created_at: '2018-01-01T12:00:00.000Z'
}
/**
 *  Returns an instance of Person like
 * {
 *   name:'Jhon',
 *   lastName: 'Smith',
 *   age: 25,
 *   gender: 'M',
 *   createdAt: Mon Jan 01 2018 08:00:00 GMT-0400 (Atlantic Standard Time) {}
 * }
 **/

const person = Parseus.decode(data).to(Person)
person.gender = 'F'
person.age = 18
person.name = 'Sara'

/**
 * The second parameter is optional, but if
 * the class instance has been mutated
 * we should pass the original class for references
 *
 * Returns an object with the next structure { [key:string]: any }
 * {
 *   "person_name": "Sara",
 *   "person_last_name": "Smith",
 *   "person_age": 18,
 *   "person_gender": F,
 *   "person_created_at": "2018-01-01T12:00:00.000Z"
 * }
 **/
const personMarshalled = Parseus.encode(person, Person)

Instalation

  1. Install the npm package:

    npm install parseus --save or using yarn yarn add @rijudev/parseus

  2. You need to install reflect-metadata shim:

    npm install reflect-metadata --save or using yarn yarn add reflect-metadata

    and import it somewhere in the global place of your app:

    import 'reflect-metadata'

API

FieldType

Parseus allow the next field type values:
  • string
  • number
  • decimal
  • boolean
  • unique
  • date
  • array
  • object

Field Options

PropertyDescriptionTypeDefault
typeField type. Must be one of the values from the FieldTypestring'string'
nameKey name in source object. if this value is not provided it takes the model field's name wrappedstring-
isVirtualIndicates if field's value is ignored when marshall Objectbooleanfalse
defaultIndicates the initial field's valueany-
readOnlyIndicates if field's value is read only (freeze)booleanfalse
fixedThe scale for a decimal (exact numeric) field, which represents the number of digits to the right of the decimal pointnumber6
transformerSpecifies a value transformer that is to be used to (un)marshall the current field when (un)marshallITransformer-
factoryIndicates the field's model class of targetclass-

ITransformer

PropertyDescriptionTypeDefault
toUsed to marshall data when writing to the new objectFunction-
fromUsed to unmarshall data when reading from objectFunction-

ITransformerParams

PropertyDescriptionTypeDefault
keykey name in source objectstring-
optionsField type options field'sIFieldOptions-
datacomplete mapped source objectobject-

Still in progress...

FAQs

Package last updated on 11 Dec 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc