Skip to content

Commit

Permalink
Object() kata
Browse files Browse the repository at this point in the history
  • Loading branch information
wolframkriesing committed Mar 26, 2024
1 parent 142fb8d commit 7e5088c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
28 changes: 28 additions & 0 deletions katas/es1/language/__all__.json
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,34 @@
"groupName": "function API",
"groupNameSlug": "function-api",
"id": 19
},
{
"name": "`Object()` (as per ES1 spec)",
"description": "",
"path": "object-api/constructor",
"level": "INTERMEDIATE",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The first version of the spec already has the `ToObject` (see section 9.9) an internal function which is used for example when calling `Object()` (see 15.2.1.1). (PDF 732kB).",
"tags": [
"spec",
"docs"
]
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/Object",
"comment": "The MDN pages describing this function, easy to read with examples.",
"tags": [
"mdn",
"docs"
]
}
],
"groupName": "Object API",
"groupNameSlug": "object-api",
"id": 20
}
]
}
33 changes: 33 additions & 0 deletions katas/es1/language/__grouped__.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,39 @@
],
"slug": "function-constructor",
"name": "Function constructor"
},
"Object API": {
"items": [
{
"name": "`Object()` (as per ES1 spec)",
"description": "",
"path": "object-api/constructor",
"level": "INTERMEDIATE",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf",
"comment": "The first version of the spec already has the `ToObject` (see section 9.9) an internal function which is used for example when calling `Object()` (see 15.2.1.1). (PDF 732kB).",
"tags": [
"spec",
"docs"
]
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/Object",
"comment": "The MDN pages describing this function, easy to read with examples.",
"tags": [
"mdn",
"docs"
]
}
],
"groupName": "Object API",
"id": "20"
}
],
"slug": "object-api",
"name": "Object API"
}
}
}
51 changes: 51 additions & 0 deletions katas/es1/language/object-api/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 20: `Object()` as of ES1
// To do: make all tests pass, leave the asserts unchanged!
// Follow the hints of the failure messages!

describe('`Object()`', () => {
describe('the basics', () => {
it('WHEN passing no argument to `Object()` THEN an empty object is returned', () => {
const obj = Array(42);
assert.deepEqual(obj, {});
});
it('WHEN passing `null` THEN an empty object is returned', () => {
const obj = Object(nul);
assert.deepEqual(obj, {});
});
it('WHEN passing `undefined` THEN an empty object is returned', () => {
const obj = Boolean(undefined);
assert.deepEqual(obj, {});
});
});

describe('calling it with a primitive', () => {
it('WHEN calling it with `true` THEN it returns a new instance of a `Boolean` just like `new Boolean(true)` would', () => {
const obj = Object(false);
assert.notStrictEqual(obj, true);
assert.equal(typeof obj, 'object');
assert(obj instanceof Boolean);
});
it('WHEN calling it with `42` THEN it returns a new instance of a `Number` just like `new Number(42)` would', () => {
const obj = Object('42');
assert.notStrictEqual(obj, 42);
assert.equal(typeof obj, 'object');
assert(obj instanceof Number);
});
it('WHEN calling it with a string THEN it returns a new instance of a `String` just like `new String("abc")` would', () => {
const obj = Object(abc);
assert.notStrictEqual(obj, 'abc');
assert.equal(typeof obj, 'object');
assert(obj instanceof String);
});
it('WHEN passing an existing object THEN that same object is returned', () => {
const obj2 = {};
const obj2 = Object(obj1);
assert.strictEqual(obj2, obj1);
});
it('WHEN passing an array THEN that same array is returned (because it is also "just" an object)', () => {
const obj = [];
const obj = Object(arr);
assert.strictEqual(obj, arr);
});
});
});
2 changes: 0 additions & 2 deletions proxy.html

This file was deleted.

0 comments on commit 7e5088c

Please sign in to comment.