diff --git a/modules/_findkeys.js b/modules/_findkeys.js new file mode 100644 index 000000000..b4221707f --- /dev/null +++ b/modules/_findkeys.js @@ -0,0 +1,16 @@ +import cb from './_cb.js'; +import keys from './keys.js'; + + +function findKeys(obj, predicate, context) { + predicate = cb(predicate, context); + var _keys = keys(obj), key; + var arr = []; + for (var i = 0, length = _keys.length; i < length; i++) { + key = _keys[i]; + if (predicate(obj[key], key, obj)) { + arr.push(key); + } + } + return arr; +} \ No newline at end of file diff --git a/modules/index.js b/modules/index.js index c48ca7e9e..1b9d0a37b 100644 --- a/modules/index.js +++ b/modules/index.js @@ -87,6 +87,7 @@ export { default as result } from './result.js'; export { default as uniqueId } from './uniqueId.js'; export { default as chain } from './chain.js'; export { default as iteratee } from './iteratee.js'; +export { default as transition} from './transition' // Function (ahem) Functions // ------------------------- diff --git a/modules/transition.js b/modules/transition.js new file mode 100644 index 000000000..2792b1226 --- /dev/null +++ b/modules/transition.js @@ -0,0 +1,33 @@ +export default function getTogglingfunction() { + if (arguments.length == 0) { + function inner(input) { + return !input; + } + return inner; + } else if (arguments.length != 1) { + var args = arguments; + function inner(input){ + let all_arguments = Array.from(args); + all_arguments_length = all_arguments.length; + index = all_arguments.indexOf(input); + return all_arguments[(index + 1) % all_arguments_length]; + } + return inner + } else if (arguments[0] instanceof Array){ + args = arguments[0]; + function inner(input){ + let all_arguments = args; + all_arguments_length = all_arguments.length; + index = all_arguments.indexOf(input); + return all_arguments[(index + 1) % all_arguments_length]; + } + return inner + } else { + obj = arguments[0]; + function inner(input) { + inner_obj = obj; + return inner_obj[input]; + } + return inner + } +} \ No newline at end of file diff --git a/test/utility.js b/test/utility.js index b0cd4941c..97179e1c9 100644 --- a/test/utility.js +++ b/test/utility.js @@ -465,4 +465,25 @@ assert.strictEqual(template(), '<<\nx\n>>'); }); + + QUnit.test('transition function - toggles the input in several values.', function(assert) { + // assert.expect(1); + // var template = _.template('<<\nx\n>>', null, {evaluate: /<<(.*?)>>/g}); + // assert.strictEqual(template(), '<<\nx\n>>'); + toggler = _.getTogglingfunction() + var input = true; + assert.strictEqual(toggler(input), false) + toggler = _.getTogglingfunction([1, 2, 3]) + var input = 3; + assert.strictEqual(toggler(input), 1) + toggler = _.getTogglingfunction({ + 'red': 'green', + 'yellow': 'red', + 'green': 'yellow' + }) + var input = 'green'; + assert.strictEqual(toggler(input), 'yellow') + }); + }()); +