Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. (function() {
  2. 'use strict';
  3.  
  4. _.mixin({
  5. // Similar to `_.dig` but uses `window` as the starting point object.
  6. import: function(namespace, initialValue) {
  7. return _.dig(window, namespace, true, initialValue);
  8. },
  9.  
  10. // Fetch or create the nested objects referenced in @namespace@ (String or Array)
  11. // using the @baseObject@ as the starting point object. If the last reference
  12. // in the namespace doesn't exist it'll create it and assign @_initialValue@ or {}
  13. // as the initial value. If @_create@ is false it'll just try to navigate through
  14. // the objects and return undefined if not found. If it's true it'll create the
  15. // objects while navigating through them, if they don't exist.
  16. dig: function(baseObject, namespace, _createObjects, _initialValue) {
  17. var createObjects = _.isUndefined(_createObjects) ? false : !!_createObjects;
  18. var initialValue = _.isUndefined(_initialValue) ? null : _initialValue;
  19. var names = _.isArray(namespace) ? namespace : namespace.split('.');
  20. var size = names.length;
  21.  
  22. _.find(names, function(name, index) {
  23. if (createObjects && _.isUndefined(baseObject[name])) {
  24. baseObject[name] = initialValue !== null && index === size - 1 ? initialValue : {};
  25. }
  26. baseObject = baseObject[name];
  27. if (_.isUndefined(baseObject)) { return true; }
  28. });
  29. return baseObject;
  30. }
  31. });
  32. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement