Created
January 13, 2023 08:53
-
-
Save trycf/2d6349d9a9cccc1c0312d0b59e488908 to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
function run() { | |
describe("test of array flattening functions", () => { | |
var testCases = [ | |
"empty array" = { | |
input = [], | |
expected = [] | |
}, | |
"simple array" = { | |
input = ["a", "b","c", "d"], | |
expected = ["a", "b", "c", "d"] | |
}, | |
"with embedded array" = { | |
input = ["a", ["b","c"], "d"], | |
expected = ["a", "b", "c", "d"] | |
}, | |
"with embedded struct" = { | |
input = ["a", {"b"="c"}, "d"], | |
expected = ["a", {"b"="c"}, "d"] | |
} | |
] | |
describe("Original version", () => { | |
testCases.each((testDescription, testCase) => { | |
it("flattens an array with [#testDescription#]", () => { | |
expect(originalFlatten(argumentCollection=testCase.input)).toBe(testCase.expected) | |
}) | |
}) | |
}) | |
describe("Improved version", () => { | |
testCases.each((testDescription, testCase) => { | |
it("flattens an array with [#testDescription#]", () => { | |
expect(improvedFlatten(argumentCollection=testCase.input)).toBe(testCase.expected) | |
}) | |
}) | |
}) | |
}) | |
} | |
tinyTest.runTests() | |
public array function originalFlatten() { | |
var results = arguments.reduce( | |
( reduction, key, value ) => { | |
return( reduction.append( value, isArray( value ) ) ); | |
}, | |
[] | |
); | |
return( results ); | |
} | |
public array function improvedFlatten() { | |
return arrayReduce( | |
arguments, | |
(flattened, value) => flattened.append(value, true), | |
[] | |
) | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment