Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Add reference implementation and basic test setup
Browse files Browse the repository at this point in the history
Uses especially for the reference implementation.

Closes #2, although as per #1 the TDD plan is not workable anymore.
  • Loading branch information
domenic committed Jul 30, 2014
1 parent 3f9bff3 commit 54e6825
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/node_modules/
27 changes: 27 additions & 0 deletions package.json
@@ -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"
}
}
52 changes: 52 additions & 0 deletions reference-implementation/index.js
@@ -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;
};
10 changes: 10 additions & 0 deletions test/Array.prototype.contains_length.js
@@ -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');
}
27 changes: 27 additions & 0 deletions test/Array.prototype.contains_number-this.js
@@ -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"');
}
10 changes: 10 additions & 0 deletions test/README.md
@@ -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.

0 comments on commit 54e6825

Please sign in to comment.