dottie.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. (function(undefined) {
  2. var root = this;
  3. // Weird IE shit, objects do not have hasOwn, but the prototype does...
  4. var hasOwnProp = Object.prototype.hasOwnProperty;
  5. var reverseDupArray = function (array) {
  6. var result = new Array(array.length);
  7. var index = array.length;
  8. var arrayMaxIndex = index - 1;
  9. while (index--) {
  10. result[arrayMaxIndex - index] = array[index];
  11. }
  12. return result;
  13. };
  14. var Dottie = function() {
  15. var args = Array.prototype.slice.call(arguments);
  16. if (args.length == 2) {
  17. return Dottie.find.apply(this, args);
  18. }
  19. return Dottie.transform.apply(this, args);
  20. };
  21. // Legacy syntax, changed syntax to have get/set be similar in arg order
  22. Dottie.find = function(path, object) {
  23. return Dottie.get(object, path);
  24. };
  25. // Dottie memoization flag
  26. Dottie.memoizePath = true;
  27. var memoized = {};
  28. // Traverse object according to path, return value if found - Return undefined if destination is unreachable
  29. Dottie.get = function(object, path, defaultVal) {
  30. if ((object === undefined) || (object === null) || (path === undefined) || (path === null)) {
  31. return defaultVal;
  32. }
  33. var names;
  34. if (typeof path === "string") {
  35. if (Dottie.memoizePath) {
  36. if (memoized[path]) {
  37. names = memoized[path].slice(0);
  38. } else {
  39. names = path.split('.').reverse();
  40. memoized[path] = names.slice(0);
  41. }
  42. } else {
  43. names = path.split('.').reverse();
  44. }
  45. } else if (Array.isArray(path)) {
  46. names = reverseDupArray(path);
  47. }
  48. while (names.length && (object = object[names.pop()]) !== undefined && object !== null);
  49. // Handle cases where accessing a childprop of a null value
  50. if (object === null && names.length) object = undefined;
  51. return (object === undefined ? defaultVal : object);
  52. };
  53. Dottie.exists = function(object, path) {
  54. return Dottie.get(object, path) !== undefined;
  55. };
  56. // Set nested value
  57. Dottie.set = function(object, path, value, options) {
  58. var pieces = Array.isArray(path) ? path : path.split('.'), current = object, piece, length = pieces.length;
  59. if (pieces[0] === '__proto__') return;
  60. if (typeof current !== 'object') {
  61. throw new Error('Parent is not an object.');
  62. }
  63. for (var index = 0; index < length; index++) {
  64. piece = pieces[index];
  65. // Create namespace (object) where none exists.
  66. // If `force === true`, bruteforce the path without throwing errors.
  67. if (
  68. !hasOwnProp.call(current, piece)
  69. || current[piece] === undefined
  70. || ((typeof current[piece] !== 'object' || current[piece] === null) && options && options.force === true)) {
  71. current[piece] = {};
  72. }
  73. if (index == (length - 1)) {
  74. // Set final value
  75. current[piece] = value;
  76. } else {
  77. // We do not overwrite existing path pieces by default
  78. if (typeof current[piece] !== 'object' || current[piece] === null) {
  79. throw new Error('Target key "' + piece + '" is not suitable for a nested value. (It is in use as non-object. Set `force` to `true` to override.)');
  80. }
  81. // Traverse next in path
  82. current = current[piece];
  83. }
  84. }
  85. // Is there any case when this is relevant? It's also the last line in the above for-loop
  86. current[piece] = value;
  87. };
  88. // Set default nested value
  89. Dottie['default'] = function(object, path, value) {
  90. if (Dottie.get(object, path) === undefined) {
  91. Dottie.set(object, path, value);
  92. }
  93. };
  94. // Transform unnested object with .-seperated keys into a nested object.
  95. Dottie.transform = function Dottie$transformfunction(object, options) {
  96. if (Array.isArray(object)) {
  97. return object.map(function(o) {
  98. return Dottie.transform(o, options);
  99. });
  100. }
  101. options = options || {};
  102. options.delimiter = options.delimiter || '.';
  103. var pieces
  104. , piecesLength
  105. , piece
  106. , current
  107. , transformed = {}
  108. , key
  109. , keys = Object.keys(object)
  110. , length = keys.length
  111. , i;
  112. for (i = 0; i < length; i++) {
  113. key = keys[i];
  114. if (key.indexOf(options.delimiter) !== -1) {
  115. pieces = key.split(options.delimiter);
  116. if (pieces[0] === '__proto__') break;
  117. piecesLength = pieces.length;
  118. current = transformed;
  119. for (var index = 0; index < piecesLength; index++) {
  120. piece = pieces[index];
  121. if (index != (piecesLength - 1) && !current.hasOwnProperty(piece)) {
  122. current[piece] = {};
  123. }
  124. if (index == (piecesLength - 1)) {
  125. current[piece] = object[key];
  126. }
  127. current = current[piece];
  128. if (current === null) {
  129. break;
  130. }
  131. }
  132. } else {
  133. transformed[key] = object[key];
  134. }
  135. }
  136. return transformed;
  137. };
  138. Dottie.flatten = function(object, seperator) {
  139. if (typeof seperator === "undefined") seperator = '.';
  140. var flattened = {}
  141. , current
  142. , nested;
  143. for (var key in object) {
  144. if (hasOwnProp.call(object, key)) {
  145. current = object[key];
  146. if (Object.prototype.toString.call(current) === "[object Object]") {
  147. nested = Dottie.flatten(current, seperator);
  148. for (var _key in nested) {
  149. flattened[key+seperator+_key] = nested[_key];
  150. }
  151. } else {
  152. flattened[key] = current;
  153. }
  154. }
  155. }
  156. return flattened;
  157. };
  158. Dottie.paths = function(object, prefixes) {
  159. var paths = [];
  160. var value;
  161. var key;
  162. prefixes = prefixes || [];
  163. if (typeof object === 'object') {
  164. for (key in object) {
  165. value = object[key];
  166. if (typeof value === 'object' && value !== null) {
  167. paths = paths.concat(Dottie.paths(value, prefixes.concat([key])));
  168. } else {
  169. paths.push(prefixes.concat(key).join('.'));
  170. }
  171. }
  172. } else {
  173. throw new Error('Paths was called with non-object argument.');
  174. }
  175. return paths;
  176. };
  177. if (typeof module !== 'undefined' && module.exports) {
  178. exports = module.exports = Dottie;
  179. } else {
  180. root['Dottie'] = Dottie;
  181. root['Dot'] = Dottie; //BC
  182. if (typeof define === "function") {
  183. define([], function () { return Dottie; });
  184. }
  185. }
  186. })();