dottie.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 (typeof current !== 'object') {
  60. throw new Error('Parent is not an object.');
  61. }
  62. for (var index = 0; index < length; index++) {
  63. piece = pieces[index];
  64. // Create namespace (object) where none exists.
  65. // If `force === true`, bruteforce the path without throwing errors.
  66. if (!hasOwnProp.call(current, piece) || current[piece] === undefined || (typeof current[piece] !== 'object' && options && options.force === true)) {
  67. current[piece] = {};
  68. }
  69. if (index == (length - 1)) {
  70. // Set final value
  71. current[piece] = value;
  72. } else {
  73. // We do not overwrite existing path pieces by default
  74. if (typeof current[piece] !== 'object') {
  75. 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.)');
  76. }
  77. // Traverse next in path
  78. current = current[piece];
  79. }
  80. }
  81. // Is there any case when this is relevant? It's also the last line in the above for-loop
  82. current[piece] = value;
  83. };
  84. // Set default nested value
  85. Dottie['default'] = function(object, path, value) {
  86. if (Dottie.get(object, path) === undefined) {
  87. Dottie.set(object, path, value);
  88. }
  89. };
  90. // Transform unnested object with .-seperated keys into a nested object.
  91. Dottie.transform = function Dottie$transformfunction(object, options) {
  92. if (Array.isArray(object)) {
  93. return object.map(function(o) {
  94. return Dottie.transform(o, options);
  95. });
  96. }
  97. options = options || {};
  98. options.delimiter = options.delimiter || '.';
  99. var pieces
  100. , piecesLength
  101. , piece
  102. , current
  103. , transformed = {}
  104. , key
  105. , keys = Object.keys(object)
  106. , length = keys.length
  107. , i;
  108. for (i = 0; i < length; i++) {
  109. key = keys[i];
  110. if (key.indexOf(options.delimiter) !== -1) {
  111. pieces = key.split(options.delimiter);
  112. piecesLength = pieces.length;
  113. current = transformed;
  114. for (var index = 0; index < piecesLength; index++) {
  115. piece = pieces[index];
  116. if (index != (piecesLength - 1) && !current.hasOwnProperty(piece)) {
  117. current[piece] = {};
  118. }
  119. if (index == (piecesLength - 1)) {
  120. current[piece] = object[key];
  121. }
  122. current = current[piece];
  123. if (current === null) {
  124. break;
  125. }
  126. }
  127. } else {
  128. transformed[key] = object[key];
  129. }
  130. }
  131. return transformed;
  132. };
  133. Dottie.flatten = function(object, seperator) {
  134. if (typeof seperator === "undefined") seperator = '.';
  135. var flattened = {}
  136. , current
  137. , nested;
  138. for (var key in object) {
  139. if (hasOwnProp.call(object, key)) {
  140. current = object[key];
  141. if (Object.prototype.toString.call(current) === "[object Object]") {
  142. nested = Dottie.flatten(current, seperator);
  143. for (var _key in nested) {
  144. flattened[key+seperator+_key] = nested[_key];
  145. }
  146. } else {
  147. flattened[key] = current;
  148. }
  149. }
  150. }
  151. return flattened;
  152. };
  153. Dottie.paths = function(object, prefixes) {
  154. var paths = [];
  155. var value;
  156. var key;
  157. prefixes = prefixes || [];
  158. if (typeof object === 'object') {
  159. for (key in object) {
  160. value = object[key];
  161. if (typeof value === 'object' && value !== null) {
  162. paths = paths.concat(Dottie.paths(value, prefixes.concat([key])));
  163. } else {
  164. paths.push(prefixes.concat(key).join('.'));
  165. }
  166. }
  167. } else {
  168. throw new Error('Paths was called with non-object argument.');
  169. }
  170. return paths;
  171. };
  172. if (typeof module !== 'undefined' && module.exports) {
  173. exports = module.exports = Dottie;
  174. } else {
  175. root['Dottie'] = Dottie;
  176. root['Dot'] = Dottie; //BC
  177. if (typeof define === "function") {
  178. define([], function () { return Dottie; });
  179. }
  180. }
  181. })();