548e3ee4b66baca2b37ab46ac06a6db52323da9d
[platform/framework/web/crosswalk-tizen.git] /
1 'use strict';
2
3 var isPlainObject = require('./is-plain-object')
4   , value         = require('./valid-value')
5
6   , keys = Object.keys
7   , copy;
8
9 copy = function (source) {
10         var target = {};
11         this[0].push(source);
12         this[1].push(target);
13         keys(source).forEach(function (key) {
14                 var index;
15                 if (!isPlainObject(source[key])) {
16                         target[key] = source[key];
17                         return;
18                 }
19                 index = this[0].indexOf(source[key]);
20                 if (index === -1) target[key] = copy.call(this, source[key]);
21                 else target[key] = this[1][index];
22         }, this);
23         return target;
24 };
25
26 module.exports = function (source) {
27         var obj = Object(value(source));
28         if (obj !== source) return obj;
29         return copy.call([[], []], obj);
30 };