Skip to content

Commit

Permalink
Fix converting issue at array type
Browse files Browse the repository at this point in the history
  • Loading branch information
kination committed Jun 11, 2023
1 parent 16da150 commit 60f331b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
25 changes: 21 additions & 4 deletions src/tucson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ export class Tucson {
for (const key of Object.keys(obj)) {
const value: any = obj[key]

// if value is `object`, recursively call 'formed' to convert inside object
// if value is `object`,
// if type is 'Date', don't make conversion
// if type is 'Array', convert items with loop
// else type is common object, recursively call 'formed' to convert inside object
if (typeof value === 'object') {
const formedResult = {}
this.formed(value, option, formedResult)
instance[converter(this.config.keyConvert, key)] = formedResult
if (value instanceof Date) {
instance[converter(this.config.keyConvert, key)] = value
} else if(value instanceof Array) {
const res = []
for (const v of value) {
const formedResult = {}
this.formed(v, option, formedResult)
res.push(formedResult)
}

instance[converter(this.config.keyConvert, key)] = res
} else {
const formedResult = {}
this.formed(value, option, formedResult)
instance[converter(this.config.keyConvert, key)] = formedResult
}

continue
}

Expand Down
43 changes: 43 additions & 0 deletions test/testArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
let assert = require('assert');
let moment = require('moment')
let { Tucson, Config } = require('../lib/index')

describe('Check array', () => {
const dummyUnderscoreJson = {
id: 1,
company_id: 1,
title: 'Software engineer',
description: 'Job desc',
location: 'Anywhere',
timezone: 'AZ',
contact_email: 'admin@remozine.com',
tags: [],
published: true,
company: [
{
company_name: 'remozine',
company_url: 'www.remozine.com',
company_logo_url: null
},
{
company_name: 'google',
company_url: 'google.com',
company_logo_url: null
}
]
}
let formedResult = {}
let cctucson = new Tucson(new Config('camelCase'))

before(() => {
cctucson.formed(dummyUnderscoreJson, {}, formedResult)
})

describe('check array values', () => {
it('data inside array value should be converted ', function() {
assert(formedResult["company"] instanceof Array === true)
assert(formedResult["company"][0]["companyName"] === "remozine")
assert(formedResult["company"][1]["companyName"] === "google")
});
});
});
2 changes: 1 addition & 1 deletion test/testCamelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let assert = require('assert');
let moment = require('moment')
let { Tucson, Config } = require('../lib/index')

describe('to camelcase', () => {
describe('Check camelcase', () => {

const dummyUnderscoreJson = {
'group_code': 17,
Expand Down
31 changes: 31 additions & 0 deletions test/testDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let assert = require('assert');
let moment = require('moment')
let { Tucson, Config } = require('../lib/index')

describe('Check date', () => {

const dummyUnderscoreJson = {
id: 1,
company_id: 1,
title: 'Software engineer',
description: 'Job desc',
location: 'Anywhere',
timezone: 'AZ',
contact_email: 'admin@remozine.com',
created_at: new Date('2023-05-28 09:00:00'),
updated_at: new Date('2023-05-28 09:00:00'),
published: true
}
let formedResult = {}
let cctucson = new Tucson(new Config('camelCase'))

before(() => {
cctucson.formed(dummyUnderscoreJson, {}, formedResult)
})

describe('check date', () => {
it('value with type `Date` should not be converted', function() {
assert(formedResult['createdAt'].toLocaleString() === "5/28/2023, 9:00:00 AM")
});
});
});
2 changes: 1 addition & 1 deletion test/testRecursive.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let assert = require('assert')
let { Tucson, Config } = require('../lib/index')

describe('test recursive data', () => {
describe('Check recursive data', () => {

const dummyUnderscoreJson = {
'group_code': 17,
Expand Down

0 comments on commit 60f331b

Please sign in to comment.