thx!
function toPrimitiveDeep(stringObject){ let result try{ if (stringObject==='undefined') return undefined // :) result = JSON.parse(stringObject); }catch (e){ console.log(`error`,e) return stringObject } if(typeof result !== 'object' || result===null) return result let keys = Object.keys(result) keys.forEach((key)=>{ result[key] = toPrimitiveDeep(result[key]) }) return result }
- function toPrimitiveDeep(stringObject){
- let result
- try{
- if (stringObject==='undefined') return undefined // :)
- result = JSON.parse(stringObject);
- }catch (e){
- console.log(`error`,e)
- return stringObject
- }
if(typeof result !== 'object') return result- if(typeof result !== 'object' || result===null) return result
- let keys = Object.keys(result)
- keys.forEach((key)=>{
- result[key] = toPrimitiveDeep(result[key])
- })
- return result
- }
const chai = require('chai') const assert = chai.assert const stgfy= JSON.stringify describe('Solution', function () { it('should test for something', function () { const obj1 = `{"a":"false","b":"33","c":"hi i\'m C!","d":"{\\"d1\\":\\"4.5\\",\\"d2\\":\\"Yo D2 here\\"}"}` assert.strictEqual( stgfy(toPrimitiveDeep(obj1)), stgfy({ a: false, b: 33, c: "hi i'm C!", d: { d1: 4.5, d2: 'Yo D2 here', }, }), ) }), it('should return the string when string is passed',()=>{ let aString = 'a string' assert.strictEqual(toPrimitiveDeep(aString),aString) }) it('should return boolean when get string as "true" or "false"',()=>{ let thaBoolean = Math.random()<0.45 assert.strictEqual(toPrimitiveDeep(thaBoolean), thaBoolean) }) it('should return a deep object with a value diferent of a string when it can be interpreted as number or boolean', ()=>{ let thaBoolean = Math.random()<0.45 let thaNumba = Math.random() *100 %10 let answer = { a:thaBoolean, b:{ ba:thaNumba, bb:{ bba:thaBoolean, bbb:thaNumba } } } let question = stgfy({ a:thaBoolean.toString(), b:stgfy({ ba:thaNumba.toString(), bb:stgfy({ bba:thaBoolean.toString(), bbb:thaNumba.toString() }) }) }) console.log(question, answer) assert.strictEqual(stgfy(toPrimitiveDeep(question)), stgfy(answer)) }) it('should work for "undefined"', () => { assert.strictEqual(toPrimitiveDeep("undefined"), undefined) }) it('should work for "null"', () => { assert.strictEqual(toPrimitiveDeep("null"), null) }) it('should work for "NaN"', () => { assert.strictEqual(isNaN(toPrimitiveDeep("NaN")), true) }) })
// Since Node 10, we"re using Mocha.// You can use `chai` for assertions.// Uncomment the following line to disable truncating failure messages for deep equals, do:// chai.config.truncateThreshold = 0;// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.// Uncomment the following to use the old assertions:// const Test = require("@codewars/test-compat");- const chai = require('chai')
- const assert = chai.assert
- const stgfy= JSON.stringify
- describe('Solution', function () {
- it('should test for something', function () {
- const obj1 = `{"a":"false","b":"33","c":"hi i\'m C!","d":"{\\"d1\\":\\"4.5\\",\\"d2\\":\\"Yo D2 here\\"}"}`
- assert.strictEqual(
- stgfy(toPrimitiveDeep(obj1)),
- stgfy({
- a: false,
- b: 33,
- c: "hi i'm C!",
- d: {
- d1: 4.5,
- d2: 'Yo D2 here',
- },
- }),
- )
- }),
- it('should return the string when string is passed',()=>{
- let aString = 'a string'
- assert.strictEqual(toPrimitiveDeep(aString),aString)
}),- })
- it('should return boolean when get string as "true" or "false"',()=>{
- let thaBoolean = Math.random()<0.45
- assert.strictEqual(toPrimitiveDeep(thaBoolean), thaBoolean)
}),- })
- it('should return a deep object with a value diferent of a string when it can be interpreted as number or boolean', ()=>{
- let thaBoolean = Math.random()<0.45
- let thaNumba = Math.random() *100 %10
- let answer = {
- a:thaBoolean,
- b:{
- ba:thaNumba,
- bb:{
- bba:thaBoolean,
- bbb:thaNumba
- }
- }
- }
- let question = stgfy({
- a:thaBoolean.toString(),
- b:stgfy({
- ba:thaNumba.toString(),
- bb:stgfy({
- bba:thaBoolean.toString(),
- bbb:thaNumba.toString()
- })
- })
- })
- console.log(question, answer)
- assert.strictEqual(stgfy(toPrimitiveDeep(question)), stgfy(answer))
- })
- it('should work for "undefined"', () => {
- assert.strictEqual(toPrimitiveDeep("undefined"), undefined)
- })
- it('should work for "null"', () => {
- assert.strictEqual(toPrimitiveDeep("null"), null)
- })
- it('should work for "NaN"', () => {
- assert.strictEqual(isNaN(toPrimitiveDeep("NaN")), true)
- })
- })
given that you have:
stringObject = `{
\"a\":\"false\",
\"b\":\"33\",
\"c\":\"hi i'm C!\",
\"d\":\"{
\\\"d1\\\":\\\"4.5\\\",
\\\"d2\\\":\\\"Yo D2 here\\\"
}\"
}`
make it deeply primitive as such:
resultObject = {
a: false,
b: 33,
c: "hi i'm C!",
d:{
d1:4.5,
d2:"Yo D2 here"
}
}
function toPrimitiveDeep(stringObject){
let result
try{
result = JSON.parse(stringObject);
}catch (e){
return stringObject
}
if(typeof result !== 'object') return result
let keys = Object.keys(result)
keys.forEach((key)=>{
result[key] = toPrimitiveDeep(result[key])
})
return result
}
// Since Node 10, we"re using Mocha.
// You can use `chai` for assertions.
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
const chai = require('chai')
const assert = chai.assert
const stgfy= JSON.stringify
describe('Solution', function () {
it('should test for something', function () {
const obj1 = `{"a":"false","b":"33","c":"hi i\'m C!","d":"{\\"d1\\":\\"4.5\\",\\"d2\\":\\"Yo D2 here\\"}"}`
assert.strictEqual(
stgfy(toPrimitiveDeep(obj1)),
stgfy({
a: false,
b: 33,
c: "hi i'm C!",
d: {
d1: 4.5,
d2: 'Yo D2 here',
},
}),
)
}),
it('should return the string when string is passed',()=>{
let aString = 'a string'
assert.strictEqual(toPrimitiveDeep(aString),aString)
}),
it('should return boolean when get string as "true" or "false"',()=>{
let thaBoolean = Math.random()<0.45
assert.strictEqual(toPrimitiveDeep(thaBoolean), thaBoolean)
}),
it('should return a deep object with a value diferent of a string when it can be interpreted as number or boolean', ()=>{
let thaBoolean = Math.random()<0.45
let thaNumba = Math.random() *100 %10
let answer = {
a:thaBoolean,
b:{
ba:thaNumba,
bb:{
bba:thaBoolean,
bbb:thaNumba
}
}
}
let question = stgfy({
a:thaBoolean.toString(),
b:stgfy({
ba:thaNumba.toString(),
bb:stgfy({
bba:thaBoolean.toString(),
bbb:thaNumba.toString()
})
})
})
console.log(question, answer)
assert.strictEqual(stgfy(toPrimitiveDeep(question)), stgfy(answer))
})
})