+33
-45
@@ -1,31 +0,31 @@ | ||
| var duration = require('moment').duration | ||
| const {duration} = require('moment') | ||
| /* https://github.com/moment/moment/blob/develop/src/lib/duration/create.js */ | ||
| const isoRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/ | ||
| const isoRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/ | ||
| const regexpRegex = /^\/.*\/[gimuy]*$/ | ||
| // Add a JSON representation for RegExp objects | ||
| if(RegExp.prototype.toJSON === undefined) | ||
| RegExp.prototype.toJSON = RegExp.prototype.toString | ||
| /** | ||
| * Check if an object is a number or represent one of them | ||
| * Create Javascript objects from the string fields on a JSON object | ||
| * | ||
| * @param {string} key | ||
| * key of the JSON object, ignored | ||
| * @param {*} value | ||
| * | ||
| * @return {Boolean} | ||
| * value to be converted in a Javascript object | ||
| */ | ||
| function isNumber(value) | ||
| function reviver(key, value) | ||
| { | ||
| return !isNaN(value) | ||
| return string2js(value) | ||
| } | ||
| /** | ||
| * Create Javascript objects and primitives from their string representation | ||
| * | ||
| * Promote the string fields to Javascript objects when the server returns a | ||
| * JSON with only strings on its values. The current promotions are {Boolean}, | ||
| * {Number}, {RegExp} {duration} and {Date}. | ||
| * Promote the string value to a Javascript object. The current promotions are | ||
| * {undefined}, {RegExp}, {moment.duration}, {Date} and {JSON} (that includes | ||
| * {Boolean}, {Number} and {null}). In case a promotion is not possible, it | ||
| * return the value as a {string}. | ||
| * | ||
@@ -37,43 +37,31 @@ * @param {*} value | ||
| { | ||
| if(typeof value === 'string') | ||
| { | ||
| // Boolean | ||
| if(value === 'false') return false | ||
| if(value === 'true') return true | ||
| // Value is not a string, return it directly | ||
| if(typeof value !== 'string') return value | ||
| // Number | ||
| if(isNumber(value)) return parseFloat(value) | ||
| // Undefined | ||
| if(value === 'undefined') return | ||
| // RegExp | ||
| var length = value.length | ||
| if(length > 1 | ||
| && '/' === value[0] | ||
| && '/' === value[length-1]) | ||
| return RegExp(value) | ||
| // RegExp | ||
| if(value.match(regexpRegex)) return RegExp(value) | ||
| // ISO8601 duration | ||
| if(value.match(isoRegex)) return duration(value) | ||
| // ISO8601 duration | ||
| if(value.match(isoRegex)) return duration(value) | ||
| // Date | ||
| var date = new Date(value) | ||
| if(date.toString() !== 'Invalid Date') return date | ||
| // Date | ||
| const date = new Date(value) | ||
| if(date.toString() !== 'Invalid Date') return date | ||
| // JSON | ||
| try | ||
| { | ||
| return JSON.parse(value, reviver) | ||
| } | ||
| catch(e){} | ||
| // Regular string | ||
| return value | ||
| } | ||
| string2js.reviver = reviver | ||
| /** | ||
| * Create Javascript objects from the string fields on a JSON object | ||
| * | ||
| * @param {string} key | ||
| * key of the JSON object, ignored | ||
| * @param {*} value | ||
| * value to be converted in a Javascript object | ||
| */ | ||
| string2js.reviver = function(key, value) | ||
| { | ||
| return string2js(value) | ||
| } | ||
| module.exports = string2js |
+2
-2
| { | ||
| "name": "string2js", | ||
| "version": "0.0.0", | ||
| "version": "0.1.0", | ||
| "description": "Create Javascript objects and primitives from their string representation", | ||
@@ -26,4 +26,4 @@ "main": "index.js", | ||
| "dependencies": { | ||
| "moment": "^2.13.0" | ||
| "moment": "^2.22.0" | ||
| } | ||
| } |
+14
-0
| # string2js | ||
| Create Javascript objects and primitives from their string representation | ||
| This module expose a function to convert string values to Javascript objects. It | ||
| also offer a `reviver()` function to be used with `JSON.parse()`. | ||
| ## RegExp | ||
| The module allow to reviver `RegExp` objects. To serialize them automatically as | ||
| JSON, just add the next snippet to the begining of your code: | ||
| ```js | ||
| if(RegExp.prototype.toJSON === undefined) | ||
| RegExp.prototype.toJSON = RegExp.prototype.toString | ||
| ``` |
Sorry, the diff of this file is not supported yet
17
466.67%3549
-8.27%4
-20%51
-19.05%Updated