From fc3d6a0f8faf979bd6903a8167c56d25cdf319fc Mon Sep 17 00:00:00 2001 From: mohammad ali seif kashani Date: Fri, 18 Dec 2020 21:46:16 +0330 Subject: [PATCH 1/3] transition function implemented #2850 --- modules/_transition.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 modules/_transition.js diff --git a/modules/_transition.js b/modules/_transition.js new file mode 100644 index 000000000..f9c8fa30f --- /dev/null +++ b/modules/_transition.js @@ -0,0 +1,33 @@ +export 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 From c3967228dbedea322688e0f7ec7f4eb22083d69a Mon Sep 17 00:00:00 2001 From: mohammad ali seif kashani Date: Sun, 20 Dec 2020 23:58:20 +0330 Subject: [PATCH 2/3] findKeys implemented --- modules/_findkeys.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 modules/_findkeys.js 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 From 9f03948bfacc3610c7f0c9f16e0ac55d4fd54e08 Mon Sep 17 00:00:00 2001 From: mohammad ali seif kashani Date: Fri, 8 Jan 2021 14:44:11 +0330 Subject: [PATCH 3/3] transition function added to index.js + test implemented for this function --- modules/index.js | 1 + modules/{_transition.js => transition.js} | 2 +- test/utility.js | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) rename modules/{_transition.js => transition.js} (95%) 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 similarity index 95% rename from modules/_transition.js rename to modules/transition.js index f9c8fa30f..2792b1226 100644 --- a/modules/_transition.js +++ b/modules/transition.js @@ -1,4 +1,4 @@ -export function getTogglingfunction() { +export default function getTogglingfunction() { if (arguments.length == 0) { function inner(input) { return !input; 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') + }); + }()); +