Files
30-seconds-of-code/node_modules/relay-runtime/lib/stableCopy.js
2019-08-20 15:52:05 +02:00

36 lines
772 B
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
'use strict';
/**
* Creates a copy of the provided value, ensuring any nested objects have their
* keys sorted such that equivalent values would have identical JSON.stringify
* results.
*/
function stableCopy(value) {
if (!value || typeof value !== 'object') {
return value;
}
if (Array.isArray(value)) {
return value.map(stableCopy);
}
var keys = Object.keys(value).sort();
var stable = {};
for (var i = 0; i < keys.length; i++) {
stable[keys[i]] = stableCopy(value[keys[i]]);
}
return stable;
}
module.exports = stableCopy;