Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- 'use strict';
- _.mixin({
- // Similar to `_.dig` but uses `window` as the starting point object.
- import: function(namespace, initialValue) {
- return _.dig(window, namespace, true, initialValue);
- },
- // Fetch or create the nested objects referenced in @namespace@ (String or Array)
- // using the @baseObject@ as the starting point object. If the last reference
- // in the namespace doesn't exist it'll create it and assign @_initialValue@ or {}
- // as the initial value. If @_create@ is false it'll just try to navigate through
- // the objects and return undefined if not found. If it's true it'll create the
- // objects while navigating through them, if they don't exist.
- dig: function(baseObject, namespace, _createObjects, _initialValue) {
- var createObjects = _.isUndefined(_createObjects) ? false : !!_createObjects;
- var initialValue = _.isUndefined(_initialValue) ? null : _initialValue;
- var names = _.isArray(namespace) ? namespace : namespace.split('.');
- var size = names.length;
- _.find(names, function(name, index) {
- if (createObjects && _.isUndefined(baseObject[name])) {
- baseObject[name] = initialValue !== null && index === size - 1 ? initialValue : {};
- }
- baseObject = baseObject[name];
- if (_.isUndefined(baseObject)) { return true; }
- });
- return baseObject;
- }
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement