123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- var cycle = exports;
- cycle.decycle = function decycle(object) {
- 'use strict';
- var objects = [],
- paths = [];
- return (function derez(value, path) {
- var i,
- name,
- nu;
- if (typeof value === 'object' && value !== null &&
- !(value instanceof Boolean) &&
- !(value instanceof Date) &&
- !(value instanceof Number) &&
- !(value instanceof RegExp) &&
- !(value instanceof String)) {
- for (i = 0; i < objects.length; i += 1) {
- if (objects[i] === value) {
- return {$ref: paths[i]};
- }
- }
- objects.push(value);
- paths.push(path);
- if (Object.prototype.toString.apply(value) === '[object Array]') {
- nu = [];
- for (i = 0; i < value.length; i += 1) {
- nu[i] = derez(value[i], path + '[' + i + ']');
- }
- } else {
- nu = {};
- for (name in value) {
- if (Object.prototype.hasOwnProperty.call(value, name)) {
- nu[name] = derez(value[name],
- path + '[' + JSON.stringify(name) + ']');
- }
- }
- }
- return nu;
- }
- return value;
- }(object, '$'));
- };
- cycle.retrocycle = function retrocycle($) {
- 'use strict';
- var px =
- /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
- (function rez(value) {
- var i, item, name, path;
- if (value && typeof value === 'object') {
- if (Object.prototype.toString.apply(value) === '[object Array]') {
- for (i = 0; i < value.length; i += 1) {
- item = value[i];
- if (item && typeof item === 'object') {
- path = item.$ref;
- if (typeof path === 'string' && px.test(path)) {
- value[i] = eval(path);
- } else {
- rez(item);
- }
- }
- }
- } else {
- for (name in value) {
- if (typeof value[name] === 'object') {
- item = value[name];
- if (item) {
- path = item.$ref;
- if (typeof path === 'string' && px.test(path)) {
- value[name] = eval(path);
- } else {
- rez(item);
- }
- }
- }
- }
- }
- }
- }($));
- return $;
- };
|