diff --git a/samples/JavaScript/namespace.js b/samples/JavaScript/namespace.js new file mode 100644 index 00000000..26d9acbd --- /dev/null +++ b/samples/JavaScript/namespace.js @@ -0,0 +1,93 @@ +(function(root, factory) { + if (typeof define === 'function' && define.amd) { + define(['lodash'], factory); + } else if (typeof exports !== 'undefined') { + module.exports = factory(require('lodash')); + } else { + root.Namespace = factory(root._); + } +})(this, function(_) { + 'use strict'; + + /** + * @module namespace + * @class namespace + */ + function Namespace() {} + + /** + * Regex for splitting keypaths into arrays. + * + * @private + * @const {RegExp} + * @type + */ + var KEYPATH_SPLITTER = /\./g; + + /** + * An internal cache to avoid calculating a keypath more than once. + * + * @private + * @type {Object} + */ + var _keypaths = {}; + + _.extend(Namespace.prototype, { + + /** + * Adds a definition to the namespace object. + * + * @public + * @instance + * @method add + * @param {String} keypath - The keypath for the definition to be added at. + * @param {Function|Object} definition - The definition to be added. + * @return {Function|Object} - The definition. + */ + add: function(keypath, definition) { + return this._walk(keypath, function(memo, name, index, keypath) { + if (index + 1 === keypath.length) { + memo[name] = _.extend(definition, memo[name]); + } + return memo[name] || (memo[name] = {}); + }); + }, + + /** + * Retrieves a definition from the namespace safely. + * + * @public + * @instance + * @method get + * @param {String} keypath - The keypath to lookup a definition for. + * @returns {Function|Object|undefined} - The definition if it exists, otherwise `undefined`. + */ + get: function(keypath) { + return this._walk(keypath); + }, + + /** + * An internal function for walking a keypath. + * + * @private + * @instance + * @method _walk + * @param {String} keypath - The keypath to walk through. + * @param {Function} [callback] - An optional callback to be called at each item in the path. + * @returns {function|Object|undefined} - The reduced keypath. + */ + _walk: function(keypath, callback) { + return _.reduce( + _keypaths[keypath] || (_keypaths[keypath] = keypath.split(KEYPATH_SPLITTER)), + callback || function(memo, name) { + return memo && memo[name]; + }, + this + ); + } + }); + + return Namespace; +}); + +//# sourceMappingURL=namespace.js.map \ No newline at end of file diff --git a/test/test_heuristics.rb b/test/test_heuristics.rb index 92047e1c..f6de6b13 100644 --- a/test/test_heuristics.rb +++ b/test/test_heuristics.rb @@ -20,8 +20,9 @@ class TestHeuristcs < Test::Unit::TestCase Dir.glob("#{samples_path}/#{language_name}/#{file}") end - def test_no_language - results = Heuristics.call(file_blob("C++/render_adapter.cpp"), []) + def test_no_match + language = [] + results = Heuristics.call(file_blob("JavaScript/namespace.js"), language) assert_equal [], results end