diff --git a/dist/amd/can-get.js b/dist/amd/can-get.js new file mode 100644 index 0000000..9d85476 --- /dev/null +++ b/dist/amd/can-get.js @@ -0,0 +1,26 @@ +/*can-get@0.2.0#can-get*/ +define([ + 'require', + 'exports', + 'module', + 'can-reflect', + './utils' +], function (require, exports, module) { + 'use strict'; + var canReflect = require('can-reflect'); + var utils = require('./utils'); + function get(obj, name) { + var parts = utils.parts(name); + var length = parts.length, current, i, container; + if (!length) { + return obj; + } + current = obj; + for (i = 0; i < length && utils.isContainer(current) && current !== null; i++) { + container = current; + current = canReflect.getKeyValue(container, parts[i]); + } + return current; + } + module.exports = get; +}); \ No newline at end of file diff --git a/dist/amd/utils.js b/dist/amd/utils.js new file mode 100644 index 0000000..247aafc --- /dev/null +++ b/dist/amd/utils.js @@ -0,0 +1,57 @@ +/*can-get@0.2.0#utils*/ +define([ + 'require', + 'exports', + 'module', + 'can-reflect' +], function (require, exports, module) { + var canReflect = require('can-reflect'); + var utils = { + isContainer: function (current) { + var type = typeof current; + return current && (type === 'object' || type === 'function'); + }, + strReplacer: /\{([^\}]+)\}/g, + deleteAtPath: function (data, path) { + var parts = utils.parts(path); + var current = data; + for (var i = 0; i < parts.length - 1; i++) { + if (current) { + current = canReflect.getKeyValue(current, parts[i]); + } + } + if (current) { + canReflect.deleteKeyValue(current, parts[parts.length - 1]); + } + }, + parts: function (name) { + if (Array.isArray(name)) { + return name; + } else { + return typeof name !== 'undefined' ? (name + '').replace(/\[/g, '.').replace(/]/g, '').split('.') : []; + } + }, + walk: function (obj, name, keyCallback) { + var parts = utils.parts(name); + var length = parts.length, current, i, container, part; + if (!length) { + return; + } + current = obj; + for (i = 0; i < length; i++) { + container = current; + part = parts[i]; + current = utils.isContainer(container) && canReflect.getKeyValue(container, part); + var result = keyCallback({ + parent: container, + key: part, + value: current + }, i); + if (result !== undefined) { + current = result; + } + } + } + }; + module.exports = utils; +}); \ No newline at end of file diff --git a/dist/cjs/can-get.js b/dist/cjs/can-get.js new file mode 100644 index 0000000..74d57ee --- /dev/null +++ b/dist/cjs/can-get.js @@ -0,0 +1,18 @@ +/*can-get@0.2.0#can-get*/ +'use strict'; +var canReflect = require('can-reflect'); +var utils = require('./utils.js'); +function get(obj, name) { + var parts = utils.parts(name); + var length = parts.length, current, i, container; + if (!length) { + return obj; + } + current = obj; + for (i = 0; i < length && utils.isContainer(current) && current !== null; i++) { + container = current; + current = canReflect.getKeyValue(container, parts[i]); + } + return current; +} +module.exports = get; \ No newline at end of file diff --git a/dist/cjs/utils.js b/dist/cjs/utils.js new file mode 100644 index 0000000..f2358f5 --- /dev/null +++ b/dist/cjs/utils.js @@ -0,0 +1,50 @@ +/*can-get@0.2.0#utils*/ +var canReflect = require('can-reflect'); +var utils = { + isContainer: function (current) { + var type = typeof current; + return current && (type === 'object' || type === 'function'); + }, + strReplacer: /\{([^\}]+)\}/g, + deleteAtPath: function (data, path) { + var parts = utils.parts(path); + var current = data; + for (var i = 0; i < parts.length - 1; i++) { + if (current) { + current = canReflect.getKeyValue(current, parts[i]); + } + } + if (current) { + canReflect.deleteKeyValue(current, parts[parts.length - 1]); + } + }, + parts: function (name) { + if (Array.isArray(name)) { + return name; + } else { + return typeof name !== 'undefined' ? (name + '').replace(/\[/g, '.').replace(/]/g, '').split('.') : []; + } + }, + walk: function (obj, name, keyCallback) { + var parts = utils.parts(name); + var length = parts.length, current, i, container, part; + if (!length) { + return; + } + current = obj; + for (i = 0; i < length; i++) { + container = current; + part = parts[i]; + current = utils.isContainer(container) && canReflect.getKeyValue(container, part); + var result = keyCallback({ + parent: container, + key: part, + value: current + }, i); + if (result !== undefined) { + current = result; + } + } + } +}; +module.exports = utils; \ No newline at end of file diff --git a/dist/global/can-get.js b/dist/global/can-get.js new file mode 100644 index 0000000..fe69567 --- /dev/null +++ b/dist/global/can-get.js @@ -0,0 +1,223 @@ +/*[global-shim-start]*/ +(function(exports, global, doEval) { + // jshint ignore:line + var origDefine = global.define; + + var get = function(name) { + var parts = name.split("."), + cur = global, + i; + for (i = 0; i < parts.length; i++) { + if (!cur) { + break; + } + cur = cur[parts[i]]; + } + return cur; + }; + var set = function(name, val) { + var parts = name.split("."), + cur = global, + i, + part, + next; + for (i = 0; i < parts.length - 1; i++) { + part = parts[i]; + next = cur[part]; + if (!next) { + next = cur[part] = {}; + } + cur = next; + } + part = parts[parts.length - 1]; + cur[part] = val; + }; + var useDefault = function(mod) { + if (!mod || !mod.__esModule) return false; + var esProps = { __esModule: true, default: true }; + for (var p in mod) { + if (!esProps[p]) return false; + } + return true; + }; + + var hasCjsDependencies = function(deps) { + return ( + deps[0] === "require" && deps[1] === "exports" && deps[2] === "module" + ); + }; + + var modules = + (global.define && global.define.modules) || + (global._define && global._define.modules) || + {}; + var ourDefine = (global.define = function(moduleName, deps, callback) { + var module; + if (typeof deps === "function") { + callback = deps; + deps = []; + } + var args = [], + i; + for (i = 0; i < deps.length; i++) { + args.push( + exports[deps[i]] + ? get(exports[deps[i]]) + : modules[deps[i]] || get(deps[i]) + ); + } + // CJS has no dependencies but 3 callback arguments + if (hasCjsDependencies(deps) || (!deps.length && callback.length)) { + module = { exports: {} }; + args[0] = function(name) { + return exports[name] ? get(exports[name]) : modules[name]; + }; + args[1] = module.exports; + args[2] = module; + } else if (!args[0] && deps[0] === "exports") { + // Babel uses the exports and module object. + module = { exports: {} }; + args[0] = module.exports; + if (deps[1] === "module") { + args[1] = module; + } + } else if (!args[0] && deps[0] === "module") { + args[0] = { id: moduleName }; + } + + global.define = origDefine; + var result = callback ? callback.apply(null, args) : undefined; + global.define = ourDefine; + + // Favor CJS module.exports over the return value + result = module && module.exports ? module.exports : result; + modules[moduleName] = result; + + // Set global exports + var globalExport = exports[moduleName]; + if (globalExport && !get(globalExport)) { + if (useDefault(result)) { + result = result["default"]; + } + set(globalExport, result); + } + }); + global.define.orig = origDefine; + global.define.modules = modules; + global.define.amd = true; + ourDefine("@loader", [], function() { + // shim for @@global-helpers + var noop = function() {}; + return { + get: function() { + return { prepareGlobal: noop, retrieveGlobal: noop }; + }, + global: global, + __exec: function(__load) { + doEval(__load.source, global); + } + }; + }); +})( + {}, + typeof self == "object" && self.Object == Object + ? self + : typeof process === "object" && + Object.prototype.toString.call(process) === "[object process]" + ? global + : window, + function(__$source__, __$global__) { + // jshint ignore:line + eval("(function() { " + __$source__ + " \n }).call(__$global__);"); + } +); + +/*can-get@0.2.0#utils*/ +define('can-get/utils', [ + 'require', + 'exports', + 'module', + 'can-reflect' +], function (require, exports, module) { + var canReflect = require('can-reflect'); + var utils = { + isContainer: function (current) { + var type = typeof current; + return current && (type === 'object' || type === 'function'); + }, + strReplacer: /\{([^\}]+)\}/g, + deleteAtPath: function (data, path) { + var parts = utils.parts(path); + var current = data; + for (var i = 0; i < parts.length - 1; i++) { + if (current) { + current = canReflect.getKeyValue(current, parts[i]); + } + } + if (current) { + canReflect.deleteKeyValue(current, parts[parts.length - 1]); + } + }, + parts: function (name) { + if (Array.isArray(name)) { + return name; + } else { + return typeof name !== 'undefined' ? (name + '').replace(/\[/g, '.').replace(/]/g, '').split('.') : []; + } + }, + walk: function (obj, name, keyCallback) { + var parts = utils.parts(name); + var length = parts.length, current, i, container, part; + if (!length) { + return; + } + current = obj; + for (i = 0; i < length; i++) { + container = current; + part = parts[i]; + current = utils.isContainer(container) && canReflect.getKeyValue(container, part); + var result = keyCallback({ + parent: container, + key: part, + value: current + }, i); + if (result !== undefined) { + current = result; + } + } + } + }; + module.exports = utils; +}); +/*can-get@0.2.0#can-get*/ +define('can-get', [ + 'require', + 'exports', + 'module', + 'can-reflect', + 'can-get/utils' +], function (require, exports, module) { + 'use strict'; + var canReflect = require('can-reflect'); + var utils = require('can-get/utils'); + function get(obj, name) { + var parts = utils.parts(name); + var length = parts.length, current, i, container; + if (!length) { + return obj; + } + current = obj; + for (i = 0; i < length && utils.isContainer(current) && current !== null; i++) { + container = current; + current = canReflect.getKeyValue(container, parts[i]); + } + return current; + } + module.exports = get; +}); +/*[global-shim-end]*/ +(function(global) { // jshint ignore:line + global._define = global.define; + global.define = global.define.orig; +} +)(typeof self == "object" && self.Object == Object ? self : (typeof process === "object" && Object.prototype.toString.call(process) === "[object process]") ? global : window); \ No newline at end of file