Skip to content

Commit

Permalink
added support for amazon string objects
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenPioneer committed Dec 3, 2018
1 parent 781bda6 commit 9996bfc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2014 - 2018 Green Pioneer Solutions
Copyright (c) 2014 - 2019 Green Pioneer Solutions

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ yarn add auto-parse
```

## Whats New

* [#14 parse object (amazon support)](https://github.com/greenpioneersolutions/auto-parse/issues/12)
* [#12 Browser Support](https://github.com/greenpioneersolutions/auto-parse/issues/12)
* [#11 Removed Lodash](https://github.com/greenpioneersolutions/auto-parse/issues/11)

Expand Down Expand Up @@ -70,6 +70,10 @@ autoParse({
}) => {name:'jason',age:50,admin:true,grade:[80,90,100]}
autoParse('{}') => {}
autoParse('["42"]') => [42]
autoParse({test:'{\\"name\\": \"greenpioneer\",\n \"company\": true,\n \\"customers\\": 1000}'}) => { test: Object }
autoParse('{\\"name\\": \"greenpioneer\",\n \"company\": true,\n \\"customers\\": 1000}') => Object
autoParse('{\\"name\\": \"greenpioneer\",\"company\": true,\\"customers\\": 1000}') => Object
autoParse('"{"name": "greenpioneer","company": true,"customers": 1000}"') => Object
// Numbers
autoParse('NaN') => NaN
autoParse('26') => 26
Expand Down Expand Up @@ -127,7 +131,7 @@ Browser Support

The MIT License (MIT)

Copyright (c) 2014-2018 Green Pioneer
Copyright (c) 2014-2019 Green Pioneer

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ function autoParse (value, type) {
var jsonParsed = null
try {
jsonParsed = JSON.parse(value)
} catch (e) {}
} catch (e) {
try {
jsonParsed = JSON.parse(
value.trim().replace(/(\\\\")|(\\")/gi, '"').replace(/(\\n|\\\\n)/gi, '').replace(/(^"|"$)|(^'|'$)/gi, '')
)
} catch (e) { }
}
if (jsonParsed && typeof jsonParsed === 'object') {
return autoParse(jsonParsed)
}
Expand Down
30 changes: 30 additions & 0 deletions test/mocha.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,36 @@ describe('Auto Parse', function () {
it('["42"]', function () {
assert.deepEqual(autoParse('["42"]'), [42])
})
it('{test:"{"name":"gps"}"}', function () {
assert.deepEqual(autoParse({test:'{\\"name\\": \"greenpioneer\",\n \"company\": true,\n \\"customers\\": 1000}'}), { // eslint-disable-line
test: {
name: 'greenpioneer',
company: true,
customers: 1000
}
})
})
it('\\n', function () {
assert.deepEqual(autoParse('{\\"name\\": \"greenpioneer\",\n \"company\": true,\n \\"customers\\": 1000}'), { // eslint-disable-line
name: 'greenpioneer',
company: true,
customers: 1000
})
})
it('\\"', function () {
assert.deepEqual(autoParse('{\\"name\\": \"greenpioneer\",\"company\": true,\\"customers\\": 1000}'), { // eslint-disable-line
name: 'greenpioneer',
company: true,
customers: 1000
})
})
it('"{}"', function () {
assert.deepEqual(autoParse('"{"name": "greenpioneer","company": true,"customers": 1000}"'), { // eslint-disable-line
name: 'greenpioneer',
company: true,
customers: 1000
})
})
})
describe('handle NaNs', function () {
it('NaN', function () {
Expand Down

0 comments on commit 9996bfc

Please sign in to comment.