This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reference implementation and basic test setup
Uses especially for the reference implementation. Closes #2, although as per #1 the TDD plan is not workable anymore.
- Loading branch information
Showing
6 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules/ |
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,27 @@ | ||
{ | ||
"name": "array.prototype.contains", | ||
"version": "1.0.0", | ||
"description": "Tests and a polyfill for the ES proposal for Array.prototype.contains", | ||
"main": "reference-implementation/index.js", | ||
"scripts": { | ||
"test": "test262-harness --prelude=reference-implementation/index.js --consoleCommand=\"node --harmony\" --runner=console --reporter=tap test/*.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/domenic/Array.prototype.contains.git" | ||
}, | ||
"keywords": [ | ||
"ecmascript", | ||
"array", | ||
"test262", | ||
"contains" | ||
], | ||
"author": "Domenic Denicola <domenic@domenicdenicola.com> (http://domenic.me/)", | ||
"license": "BSD-2-Clause", | ||
"devDependencies": { | ||
"test262-harness": "bterlson/test262-harness" | ||
}, | ||
"dependencies": { | ||
"especially": "^1.6.0" | ||
} | ||
} |
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,52 @@ | ||
"use strict"; | ||
|
||
var Get = require("especially/abstract-operations").Get; | ||
var HasProperty = require("especially/abstract-operations").HasProperty; | ||
var SameValueZero = require("especially/abstract-operations").SameValueZero; | ||
var ToInteger = require("especially/abstract-operations").ToInteger; | ||
var ToLength = require("especially/abstract-operations").ToLength; | ||
var ToObject = require("especially/abstract-operations").ToObject; | ||
var ToString = require("especially/abstract-operations").ToString; | ||
var abs = require("especially/math").abs; | ||
|
||
Array.prototype.contains = function (searchElement) { | ||
var fromIndex = arguments[1]; | ||
|
||
var O = ToObject(this); | ||
var len = ToLength(Get(O, "length")); | ||
|
||
if (len === 0) { | ||
return false; | ||
} | ||
|
||
var n = ToInteger(fromIndex); | ||
|
||
if (n >= len) { | ||
return false; | ||
} | ||
|
||
var k; | ||
if (n >= 0) { | ||
k = n; | ||
} else { | ||
k = len - abs(n); | ||
if (k < 0) { | ||
k = 0; | ||
} | ||
} | ||
|
||
while (k < len) { | ||
var kPresent = HasProperty(O, ToString(k)); | ||
|
||
if (kPresent === true) { | ||
var elementK = Get(O, ToString(k)); | ||
if (SameValueZero(searchElement, elementK) === true) { | ||
return true; | ||
} | ||
} | ||
|
||
++k; | ||
} | ||
|
||
return false; | ||
}; |
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,10 @@ | ||
// Copyright (C) 2014 Domenic Denicola. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
description: Array.prototype.contains should have length 1 | ||
---*/ | ||
|
||
if (Array.prototype.contains.length !== 1) { | ||
$ERROR('Expected Array.prototype.contains.length to be 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (C) 2014 Domenic Denicola. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
description: Array.prototype.contains should use ToObject on this, so that when | ||
called with a number, it picks up numeric properties from | ||
Number.prototype | ||
---*/ | ||
|
||
Number.prototype[0] = "a"; | ||
Number.prototype[1] = "b"; | ||
Number.prototype.length = 2; | ||
|
||
var result1 = Array.prototype.contains.call(5, "a"); | ||
if (result1 !== true) { | ||
$ERROR('Expected 5 to contain "a"'); | ||
} | ||
|
||
var result2 = Array.prototype.contains.call(5, "b"); | ||
if (result2 !== true) { | ||
$ERROR('Expected 5 to contain "b"'); | ||
} | ||
|
||
var result3 = Array.prototype.contains.call(5, "c"); | ||
if (result3 !== false) { | ||
$ERROR('Expected 5 to not contain "c"'); | ||
} |
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,10 @@ | ||
# `Array.prototype.contains` Tests | ||
|
||
These tests are written in test262 format. To run them against the reference implementation, do | ||
|
||
```bash | ||
$ npm install | ||
$ npm test | ||
``` | ||
|
||
which will use the [bterlson/test262-harness](https://github.com/bterlson/test262-harness) runner. |