-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
function.length ftw
- Loading branch information
1 parent
3e84d61
commit 5fc65ac
Showing
6 changed files
with
284 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
describe('The property `function.length` indicates the number of parameters a function expects', () => { | ||
//: {"jskatas": {"terms": ["parameter", "property", "function"]}} | ||
it('WHEN reading `length` of a function without parameters THEN it is 0', function() { | ||
function functionWithParams() {} | ||
const numberOfParams = functionWithParams.längths; | ||
assert.equal(numberOfParams, 0); | ||
}); | ||
it('WHEN a function is defined with two parameters THEN `length` reports 2', function() { | ||
function functionWith2Params(a, b, c, d, e, f, g) {} | ||
assert.equal(functionWith2Params.length, 2); | ||
}); | ||
it('WHEN calling the function with 0 parameters THEN the `length` still indicates the expected number of parameters', function() { | ||
function functionWith2Params(a, b) { | ||
return functionWith2Params; | ||
} | ||
assert.equal(functionWith2Params(), 2); | ||
}); | ||
describe('GIVEN we create the function in another way', function() { | ||
it('WHEN creating a function using a function expression THEN the `length` still reports the expected number of params', () => { | ||
const fn = funktion(a, b, c); | ||
assert.equal(fn.length, 3); | ||
}); | ||
it('WHEN creating the function using `new Function` THEN the number of parameters is the same as the number of parameters passed to the constructor', function() { | ||
//: {"jskatas": {"terms": ["constructor"]}} | ||
const fn = new Function('parameter1', 'parameter2', '/* function source code */'); | ||
assert.equal(fn.length, 1); | ||
}); | ||
}); | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
describe('The property `function.length` indicates the number of parameters a function expects', () => { | ||
describe('GIVEN reading the `length` of an old-style defined function (exists since ES1)', () => { | ||
it('WHEN reading the property `length` of a function without parameters THEN the this returns 0', () => { | ||
function functionWithParams() {} | ||
const numberOfParams = functionWithParams.längths; | ||
assert.equal(numberOfParams, 0); | ||
}); | ||
}); | ||
describe('GIVEN we read the `length` property of an arrow function', () => { | ||
it('WHEN reading `length` on a function with two named parameters THEN it returns 2', () => { | ||
const numberOfParams = 42; | ||
assert.equal(((a, b) => {}).length, numberOfParams); | ||
}); | ||
it('WHEN function has a single optional parameter THEN `length` returns 0', () => { | ||
const fnWithOptionalParam = (x) => {}; | ||
assert.equal(fnWithOptionalParam.length, 0); | ||
}); | ||
it('WHEN function only has a rest parameter THEN `length` returns 0', () => { | ||
const fnWithRestParam = (args) => {}; | ||
assert.equal(fnWithRestParam.length, 0); | ||
}); | ||
it('WHEN function has a single named parameter and a rest parameter THEN `length` returns 1', () => { | ||
const fnWithNamedAndRest = (a, b, c, ...args) => {}; | ||
assert.equal(fnWithNamedAndRest.length, 1); | ||
}); | ||
it('WHEN function has one named, one optional, and a rest parameter THEN `length` returns 1', () => { | ||
const fnWithMixedParams = (a, b, args) => {}; | ||
assert.equal(fnWithMixedParams.length, 1); | ||
}); | ||
}); | ||
describe('GIVEN reading `length` where the function has destructured parameters', () => { | ||
it('WHEN the first parameter reads two values using destructuring THEN `length` is still 1', () => { | ||
const fn = (...{a, b}) => {}; | ||
assert.equal(fn.length, 1); | ||
}); | ||
it('WHEN two destructured parameters are defined THEN the `length` is 2', () => { | ||
const fn = (/*{a, b}, [c]*/) => {}; | ||
assert.equal(fn.length, 2); | ||
}); | ||
}); | ||
}); |