Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trycf/2d6349d9a9cccc1c0312d0b59e488908 to your computer and use it in GitHub Desktop.
Save trycf/2d6349d9a9cccc1c0312d0b59e488908 to your computer and use it in GitHub Desktop.
TryCF Gist
<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