Permalink
Browse files

Always return cloned data

  • Loading branch information...
1 parent dce2cc4 commit c1086b5b4095a89b0cd890072b1bbba7ad2fc921 @philbooth philbooth committed Sep 27, 2016
Showing with 10 additions and 6 deletions.
  1. +4 −6 lib/convict.js
  2. +5 −0 npm-shrinkwrap.json
  3. +1 −0 package.json
View
@@ -10,6 +10,7 @@ const fs = require('fs');
const validator = require('validator');
const moment = require('moment');
const minimist = require('minimist');
+const cloneDeep = require('lodash/cloneDeep');
function assert(assertion, err_msg) {
if (!assertion) {
@@ -243,7 +244,6 @@ function addDefaultValues(schema, c, instance) {
}
function isObj(o) { return (typeof o === 'object' && o !== null); }
-function isRegExp(r) { return Object.prototype.toString.call(r) === '[object RegExp]'; }
function overlay(from, to, schema) {
Object.keys(from).forEach(function(k) {
@@ -352,7 +352,7 @@ var convict = function convict(def) {
* Exports all the properties (that is the keys and their current values) as JSON
*/
getProperties: function() {
- return this._instance;
+ return cloneDeep(this._instance);
},
root: deprecate.function(function() {
return this.getProperties();
@@ -389,9 +389,8 @@ var convict = function convict(def) {
*/
get: function(path) {
var o = walk(this._instance, path);
- if (isRegExp(o)) return o;
return typeof o !== 'undefined' ?
- JSON.parse(JSON.stringify(o)) :
+ cloneDeep(o) :
void 0;
},
@@ -404,9 +403,8 @@ var convict = function convict(def) {
// FOO.properties.BAR.properties.BAZ.default
path = (path.split('.').join('.properties.')) + '.default';
var o = walk(this._schema.properties, path);
- if (isRegExp(o)) return o;
return typeof o !== 'undefined' ?
- JSON.parse(JSON.stringify(o)) :
+ cloneDeep(o) :
void 0;
},
View
@@ -12,6 +12,11 @@
"from": "json5@0.5.0",
"resolved": "https://registry.npmjs.org/json5/-/json5-0.5.0.tgz"
},
+ "lodash": {
+ "version": "4.16.2",
+ "from": "lodash@4.16.2",
+ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.16.2.tgz"
+ },
"minimist": {
"version": "1.2.0",
"from": "minimist@1.2.0",
View
@@ -43,6 +43,7 @@
"dependencies": {
"depd": "1.1.0",
"json5": "0.5.0",
+ "lodash": "4.16.2",
"minimist": "1.2.0",
"moment": "2.12.0",
"validator": "4.6.1"

0 comments on commit c1086b5

Please sign in to comment.