Skip to content

Commit

Permalink
Lots of function katas.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolframkriesing committed Oct 15, 2023
1 parent 6e0a765 commit 07b5ac0
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 43 deletions.
5 changes: 5 additions & 0 deletions katas/es1/language/Function-constructor/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('', () => {
it('Function.length is always 1, the only "required" param', () => {
assert.strictEqual(Function.length, 1);
});
});
11 changes: 11 additions & 0 deletions katas/es1/language/Function-constructor/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe('Using `new Function()` creates a new function', () => {
it('WHEN using `new Function()` THEN this is equivalent to `Function()` with the same parameters', () => {
const fn1 = new Function('a', 'b', 'return a + b');
const fn2 = Function('a', 'b', 'return a + b');
assert.deepStrictEqual(Object.getOwnPropertyDescriptors(fn1), Object.getOwnPropertyDescriptors(fn2));
});
it('num params', () => {
const fn = new Function('a, b', 'c', 'return null');
assert.strictEqual(fn.length, 3);
});
});
12 changes: 12 additions & 0 deletions katas/es1/language/Function-constructor/prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe('The `Function` prototype contains all methods and properties of a function', () => {
it('the specified methods and props', () => {
const ownPropertyNames = Object.getOwnPropertyNames(Function.prototype);
['length', 'name', 'constructor', 'apply', 'bind', 'call', 'toString']
.forEach(name => assert(ownPropertyNames.includes(name), `Failed to find "${name}".`));
});
it('the old/deprecated/inofficial methods and props', () => {
const ownPropertyNames = Object.getOwnPropertyNames(Function.prototype);
['arguments', 'caller']
.forEach(name => assert(ownPropertyNames.includes(name), `Failed to find "${name}".`));
});
});
Empty file.
Empty file.
115 changes: 114 additions & 1 deletion katas/es1/language/__all__.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
"id": 9
},
{
"name": "Unary \"+\" operator, in depth",
"name": "Unary \"+\" operator in depth",
"description": "converts its operand to the Number type",
"path": "unary-operators/plus-in-depth",
"level": "ADVANCED",
Expand Down Expand Up @@ -380,6 +380,119 @@
"groupNameSlug": "function-api",
"publishDateRfc822": "Sat, 14 Oct 2023 12:15:00 GMT",
"id": 14
},
{
"name": "`new Function()` and `Function()`",
"description": "With the `Function` constructor one can dynamically create a `Function` object.",
"path": "Function-constructor/new.js",
"level": "INTERMEDIATE",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.2.1 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function",
"comment": "The MDN pages describing this property, easy to read with examples.",
"tags": [
"mdn",
"docs"
]
}
],
"groupName": "Function constructor",
"groupNameSlug": "function-constructor",
"id": 15
},
{
"name": "`Function.length`",
"description": "The `length` property of the `Function` constructor is always 1.",
"path": "Function-constructor/length.js",
"level": "BEGINNER",
"requiresKnowledgeFrom": [
{
"bundle": "es1/language",
"id": 14
}
],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.3.2 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function constructor",
"groupNameSlug": "function-constructor",
"id": 16
},
{
"name": "`Function.prototype`",
"description": "The `prototype` property of the `Function` constructor is a `Function` object.",
"path": "Function-constructor/prototype.js",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.4 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function constructor",
"groupNameSlug": "function-constructor",
"id": 17
},
{
"name": "`function.arguments` (as per ES1 spec)",
"description": "The `arguments` property of a function is an object that contains all the arguments passed to the function.",
"path": "function-api/arguments",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.5.3 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function API",
"groupNameSlug": "function-api",
"id": 18
},
{
"name": "`function.prototype`",
"description": "The `prototype` property of a function is an object that is used to implement inheritance and shared properties.",
"path": "function-api/prototype",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.5.2 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function API",
"groupNameSlug": "function-api",
"id": 19
}
]
}
116 changes: 115 additions & 1 deletion katas/es1/language/__grouped__.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
"id": "9"
},
{
"name": "Unary \"+\" operator, in depth",
"name": "Unary \"+\" operator in depth",
"description": "converts its operand to the Number type",
"path": "unary-operators/plus-in-depth",
"level": "ADVANCED",
Expand Down Expand Up @@ -399,10 +399,124 @@
],
"groupName": "Function API",
"id": "14"
},
{
"name": "`function.arguments` (as per ES1 spec)",
"description": "The `arguments` property of a function is an object that contains all the arguments passed to the function.",
"path": "function-api/arguments",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.5.3 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function API",
"id": "18"
},
{
"name": "`function.prototype`",
"description": "The `prototype` property of a function is an object that is used to implement inheritance and shared properties.",
"path": "function-api/prototype",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.5.2 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function API",
"id": "19"
}
],
"slug": "function-api",
"name": "Function API"
},
"Function constructor": {
"items": [
{
"name": "`new Function()` and `Function()`",
"description": "With the `Function` constructor one can dynamically create a `Function` object.",
"path": "Function-constructor/new.js",
"level": "INTERMEDIATE",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.2.1 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function",
"comment": "The MDN pages describing this property, easy to read with examples.",
"tags": [
"mdn",
"docs"
]
}
],
"groupName": "Function constructor",
"id": "15"
},
{
"name": "`Function.length`",
"description": "The `length` property of the `Function` constructor is always 1.",
"path": "Function-constructor/length.js",
"level": "BEGINNER",
"requiresKnowledgeFrom": [
{
"bundle": "es1/language",
"id": 14
}
],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.3.2 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function constructor",
"id": "16"
},
{
"name": "`Function.prototype`",
"description": "The `prototype` property of the `Function` constructor is a `Function` object.",
"path": "Function-constructor/prototype.js",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The very first version of the spec defines this property already, the ES1 spec, see section 15.3.4 (PDF 732kB).",
"tags": [
"spec",
"docs"
]
}
],
"groupName": "Function constructor",
"id": "17"
}
],
"slug": "function-constructor",
"name": "Function constructor"
}
}
}
19 changes: 19 additions & 0 deletions katas/es1/language/function-api/arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
describe('The `arguments` prop - discouraged since 1997 (and might not work)', () => {
it('WHEN accessing `arguments` in strict mode THEN the JS throws', () => {
const fn = function() {}
assert.throws(() => fn.arguments);
});
it('WHEN reading `arguments` using an Object-method THEN it returns `undefined`', () => {
const fn = function() {}
assert.equal(Object.getOwnPropertyDescriptor(fn, 'arguments'), undefined);
});

it('WHEN reading `arguments` in the function body THEN it is an array-like object containing all parameters the function was called with', () => {
let args;
const fn = function() {
args = arguments;
}
fn(1, 'abc', {x: 'y'});
assert.deepStrictEqual(Array.from(args), [1, 'abc', {x: 'y'}]);
});
});
10 changes: 10 additions & 0 deletions katas/es1/language/function-api/prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
describe('The `prototype` property - the place to add shared functionality', () => {
it('WHEN defining the `prototype` on a function THEN the instance of that function inherits these properties', () => {
const fn = function() {}
fn.prototype = {
solution: 42,
};
const obj = new fn();
assert.equal(obj.solution, 42);
});
});
2 changes: 1 addition & 1 deletion katas/es11/language/string-api/matchAll-in-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// To do: make all tests pass, leave the assert lines unchanged!
// Follow the hints of the failure messages!

describe('The function `string.matchAll() - in depth`', () => {
describe('The function `string.matchAll() in depth`', () => {

it('matchAll() is lazy', () => {
// ????
Expand Down
2 changes: 1 addition & 1 deletion katas/es6/language/__all__.json
Original file line number Diff line number Diff line change
Expand Up @@ -2473,7 +2473,7 @@
"id": 84
},
{
"name": "`function.name` - in depth",
"name": "`function.name` in depth",
"description": "The `name` property is \"descriptive of the function\" the specification says.",
"path": "function-api/name-in-depth",
"level": "ADVANCED",
Expand Down
2 changes: 1 addition & 1 deletion katas/es6/language/__grouped__.json
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,7 @@
"id": "84"
},
{
"name": "`function.name` - in depth",
"name": "`function.name` in depth",
"description": "The `name` property is \"descriptive of the function\" the specification says.",
"path": "function-api/name-in-depth",
"level": "ADVANCED",
Expand Down
Loading

0 comments on commit 07b5ac0

Please sign in to comment.