0.2.2 • Published 4 years ago

macoolka-json v0.2.2

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

Parse json in TypeScript

macoolka-json is a Functional programming in TypeScript for parse json. It base in fp-ts

Installation

To install the stable version:

npm install macoolka-json

Consuming macoolka-json

Parse a json to Array

import { jsonToArray } from 'macoolka-json/lib/jsonToArray'
import { pipe } from 'fp-ts/lib/pipeable'
import * as IE from 'fp-ts/lib/IOEither'
import { omit } from 'macoolka-object'
const a1 = [{
    name: 'a1',
    recordAt: new Date(2010, 12, 1, 0, 0, 0, 0),
    opened: true,
    value: 1000,
    comment: 'c1',
    others: 'o1'
}, {
    name: 'a1',
    recordAt: new Date(2010, 12, 1, 0, 0, 0, 0),
    opened: true,
    value: 1000,
    comment: 'c1',
    others: 'o2'
}]

const jsonString = JSON.stringify(a1)
describe('json', () => {
    it('jsonToArray', () => {
        const result = pipe(
            jsonToArray({
                dateNames: ['recordAt'],
                numberNames: ['value'],
                booleanNames: ['opened'], 
                names: ['name', 'comment'],
                transform: { comment: a => a + '111' }
            })(jsonString),
            IE.map(a => {
                expect(a).toEqual(a1.map(b => omit(b, ['others'])).map(b => ({ ...b, comment: b.comment + "111" })))
            })
        )()
        expect(result._tag === 'Right').toBeTruthy()
    })
    it('jsonToArray error', () => {
        const result = pipe(
            jsonToArray({ dateNames: ['recordAt'], numberNames: ['value'], booleanNames: ['opened'], names: ['name', 'comment'], transform: { comment: a => a + '111' } })(`jsonString`),
            IE.mapLeft(a => {
                console.log(a)
            }),

        )()
        expect(result._tag === 'Left').toBeTruthy()
    })
})



# License

The MIT License (MIT)