Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for stringifyPrepare/finalize
- Loading branch information
1 parent
60b64d5
commit 9b6597c
Showing
9 changed files
with
230 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./src/finalize'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var isArray = Array.isArray; | ||
|
||
function resolve(object, path, len) { | ||
var current = object; | ||
for (var i=0; i<len; i++) { | ||
current = current[path[i]]; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
function resolveType(info) { | ||
if (info.type === 'Date') { | ||
return new Date(info.value); | ||
} else { | ||
throw new Error('Bad type'); | ||
} | ||
} | ||
|
||
module.exports = function parse(outer) { | ||
var object = outer.object; | ||
|
||
var assignments = outer.assignments; | ||
if (assignments) { | ||
for (var i=0, len=assignments.length; i<len; i++) { | ||
var assignment = assignments[i]; | ||
|
||
var rhs = assignment.r; | ||
var rhsValue; | ||
|
||
if (isArray(rhs)) { | ||
rhsValue = resolve(object, rhs, rhs.length); | ||
} else { | ||
rhsValue = resolveType(rhs); | ||
} | ||
|
||
var lhs = assignment.l; | ||
var lhsLast = lhs.length-1; | ||
|
||
if (lhsLast === -1) { | ||
return rhsValue; | ||
} else { | ||
var lhsParent = resolve(object, lhs, lhsLast); | ||
lhsParent[lhs[lhsLast]] = rhsValue; | ||
} | ||
} | ||
} | ||
|
||
return object == null ? null : object; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
'use strict'; | ||
exports.serialize = require('./serialize'); | ||
exports.stringify = require('./stringify'); | ||
exports.parse = require('./parse'); | ||
exports.parse = require('./parse'); | ||
exports.finalize = require('./finalize'); | ||
exports.stringifyPrepare = require('./stringifyPrepare'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,6 @@ | ||
var isArray = Array.isArray; | ||
|
||
function resolve(object, path, len) { | ||
var current = object; | ||
for (var i=0; i<len; i++) { | ||
current = current[path[i]]; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
function resolveType(info) { | ||
if (info.type === 'Date') { | ||
return new Date(info.value); | ||
} else { | ||
throw new Error('Bad type'); | ||
} | ||
} | ||
var finalize = require('./finalize'); | ||
|
||
module.exports = function parse(json) { | ||
var outer = JSON.parse(json); | ||
|
||
var object = outer.object; | ||
|
||
var assignments = outer.assignments; | ||
if (assignments) { | ||
for (var i=0, len=assignments.length; i<len; i++) { | ||
var assignment = assignments[i]; | ||
|
||
var rhs = assignment.r; | ||
var rhsValue; | ||
|
||
if (isArray(rhs)) { | ||
rhsValue = resolve(object, rhs, rhs.length); | ||
} else { | ||
rhsValue = resolveType(rhs); | ||
} | ||
|
||
var lhs = assignment.l; | ||
var lhsLast = lhs.length-1; | ||
|
||
if (lhsLast === -1) { | ||
return rhsValue; | ||
} else { | ||
var lhsParent = resolve(object, lhs, lhsLast); | ||
lhsParent[lhs[lhsLast]] = rhsValue; | ||
} | ||
} | ||
} | ||
|
||
return object == null ? null : object; | ||
return finalize(outer); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.