Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add browser support via umd pattern #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"boss": true,
"eqnull": true,
"node": true,
"es5": true
"es5": true,
"globals": {
"define": false,
"window": false
}
}
95 changes: 54 additions & 41 deletions lib/getobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,66 @@

'use strict';

var getobject = module.exports = {};

// Split strings on dot, but only if dot isn't preceded by a backslash. Since
// JavaScript doesn't support lookbehinds, use a placeholder for "\.", split
// on dot, then replace the placeholder character with a dot.
function getParts(str) {
return str.replace(/\\\./g, '\uffff').split('.').map(function(s) {
return s.replace(/\uffff/g, '.');
});
}

// Get the value of a deeply-nested property exist in an object.
getobject.get = function(obj, parts, create) {
if (typeof parts === 'string') {
parts = getParts(parts);
(function (factory) {
var moduleName = 'getobject';
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
window[moduleName] = factory();
}
}(function () {
var getobject = {};

var part;
while (typeof obj === 'object' && obj && parts.length) {
part = parts.shift();
if (!(part in obj) && create) {
obj[part] = {};
}
obj = obj[part];
// Split strings on dot, but only if dot isn't preceded by a backslash. Since
// JavaScript doesn't support lookbehinds, use a placeholder for "\.", split
// on dot, then replace the placeholder character with a dot.
function getParts(str) {
return str.replace(/\\\./g, '\uffff').split('.').map(function(s) {
return s.replace(/\uffff/g, '.');
});
}

return obj;
};
// Get the value of a deeply-nested property exist in an object.
getobject.get = function(obj, parts, create) {
if (typeof parts === 'string') {
parts = getParts(parts);
}

var part;
while (typeof obj === 'object' && obj && parts.length) {
part = parts.shift();
if (!(part in obj) && create) {
obj[part] = {};
}
obj = obj[part];
}

// Set a deeply-nested property in an object, creating intermediary objects
// as we go.
getobject.set = function(obj, parts, value) {
parts = getParts(parts);
return obj;
};

var prop = parts.pop();
obj = getobject.get(obj, parts, true);
if (obj && typeof obj === 'object') {
return (obj[prop] = value);
}
};
// Set a deeply-nested property in an object, creating intermediary objects
// as we go.
getobject.set = function(obj, parts, value) {
parts = getParts(parts);

var prop = parts.pop();
obj = getobject.get(obj, parts, true);
if (obj && typeof obj === 'object') {
return (obj[prop] = value);
}
};

// Does a deeply-nested property exist in an object?
getobject.exists = function(obj, parts) {
parts = getParts(parts);

// Does a deeply-nested property exist in an object?
getobject.exists = function(obj, parts) {
parts = getParts(parts);
var prop = parts.pop();
obj = getobject.get(obj, parts);

var prop = parts.pop();
obj = getobject.get(obj, parts);
return typeof obj === 'object' && obj && prop in obj;
};

return typeof obj === 'object' && obj && prop in obj;
};
return getobject;
}));