Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 4bd370c2c3c212eff2fa91a93e8c4b2901ecea8f 0 parents
Tim Oxley authored
2  .gitignore
@@ -0,0 +1,2 @@
+components
+build
0  History.md
No changes.
11 Makefile
@@ -0,0 +1,11 @@
+
+build: components index.js
+ @component build --dev
+
+components: component.json
+ @component install --dev
+
+clean:
+ rm -fr build components template.js
+
+.PHONY: clean
16 Readme.md
@@ -0,0 +1,16 @@
+
+# to-array
+
+ Try convert an object into an Array
+
+## Installation
+
+ $ component install timoxley/to-array
+
+## API
+
+
+
+## License
+
+ MIT
17 component.json
@@ -0,0 +1,17 @@
+{
+ "name": "to-array",
+ "repo": "timoxley/to-array",
+ "description": "Convert an array-like object into an Array.",
+ "version": "0.0.1",
+ "keywords": [],
+ "dependencies": {
+ },
+ "development": {
+ "timoxley/assert": "*",
+ "component/domify": "*"
+ },
+ "license": "MIT",
+ "scripts": [
+ "index.js"
+ ]
+}
20 index.js
@@ -0,0 +1,20 @@
+/**
+ * Convert an array-like object into an `Array`.
+ * If `collection` is already an `Array`, then will return a clone of `collection`.
+ *
+ * @param {Array | Mixed} collection An `Array` or array-like object to convert e.g. `arguments` or `NodeList`
+ * @return {Array} Naive conversion of `collection` to a new `Array`.
+ * @api private
+ */
+
+module.exports = function toArray(collection) {
+ if (Array.isArray(collection)) return collection.slice()
+ var arr = []
+ if (!collection || !collection.length) return arr
+ for (var i = 0; i < collection.length; i++) {
+ if (collection.hasOwnProperty(i) || i in collection) {
+ arr.push(collection[i])
+ }
+ }
+ return arr
+}
203 test/mocha.css
@@ -0,0 +1,203 @@
+@charset "UTF-8";
+body {
+ font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif;
+ padding: 60px 50px;
+}
+
+#mocha ul, #mocha li {
+ margin: 0;
+ padding: 0;
+}
+
+#mocha ul {
+ list-style: none;
+}
+
+#mocha h1, #mocha h2 {
+ margin: 0;
+}
+
+#mocha h1 {
+ margin-top: 15px;
+ font-size: 1em;
+ font-weight: 200;
+}
+
+#mocha h1 a {
+ text-decoration: none;
+ color: inherit;
+}
+
+#mocha h1 a:hover {
+ text-decoration: underline;
+}
+
+#mocha .suite .suite h1 {
+ margin-top: 0;
+ font-size: .8em;
+}
+
+.hidden {
+ display: none;
+}
+
+#mocha h2 {
+ font-size: 12px;
+ font-weight: normal;
+ cursor: pointer;
+}
+
+#mocha .suite {
+ margin-left: 15px;
+}
+
+#mocha .test {
+ margin-left: 15px;
+}
+
+#mocha .test:hover h2::after {
+ position: relative;
+ top: 0;
+ right: -10px;
+ content: '(view source)';
+ font-size: 12px;
+ font-family: arial;
+ color: #888;
+}
+
+#mocha .test.pending:hover h2::after {
+ content: '(pending)';
+ font-family: arial;
+}
+
+#mocha .test.pass.medium .duration {
+ background: #C09853;
+}
+
+#mocha .test.pass.slow .duration {
+ background: #B94A48;
+}
+
+#mocha .test.pass::before {
+ content: '';
+ font-size: 12px;
+ display: block;
+ float: left;
+ margin-right: 5px;
+ color: #00d6b2;
+}
+
+#mocha .test.pass .duration {
+ font-size: 9px;
+ margin-left: 5px;
+ padding: 2px 5px;
+ color: white;
+ -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
+ -moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
+ box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
+ -webkit-border-radius: 5px;
+ -moz-border-radius: 5px;
+ -ms-border-radius: 5px;
+ -o-border-radius: 5px;
+ border-radius: 5px;
+}
+
+#mocha .test.pass.fast .duration {
+ display: none;
+}
+
+#mocha .test.pending {
+ color: #0b97c4;
+}
+
+#mocha .test.pending::before {
+ content: '';
+ color: #0b97c4;
+}
+
+#mocha .test.fail {
+ color: #c00;
+}
+
+#mocha .test.fail pre {
+ color: black;
+}
+
+#mocha .test.fail::before {
+ content: '';
+ font-size: 12px;
+ display: block;
+ float: left;
+ margin-right: 5px;
+ color: #c00;
+}
+
+#mocha .test pre.error {
+ color: #c00;
+}
+
+#mocha .test pre {
+ display: inline-block;
+ font: 12px/1.5 monaco, monospace;
+ margin: 5px;
+ padding: 15px;
+ border: 1px solid #eee;
+ border-bottom-color: #ddd;
+ -webkit-border-radius: 3px;
+ -webkit-box-shadow: 0 1px 3px #eee;
+}
+
+#report.pass .test.fail {
+ display: none;
+}
+
+#report.fail .test.pass {
+ display: none;
+}
+
+#error {
+ color: #c00;
+ font-size: 1.5 em;
+ font-weight: 100;
+ letter-spacing: 1px;
+}
+
+#stats {
+ position: fixed;
+ top: 15px;
+ right: 10px;
+ font-size: 12px;
+ margin: 0;
+ color: #888;
+}
+
+#stats .progress {
+ float: right;
+ padding-top: 0;
+}
+
+#stats em {
+ color: black;
+}
+
+#stats a {
+ text-decoration: none;
+ color: inherit;
+}
+
+#stats a:hover {
+ border-bottom: 1px solid #eee;
+}
+
+#stats li {
+ display: inline-block;
+ margin: 0 5px;
+ list-style: none;
+ padding-top: 11px;
+}
+
+code .comment { color: #ddd }
+code .init { color: #2F6FAD }
+code .string { color: #5890AD }
+code .keyword { color: #8A6343 }
+code .number { color: #2F6FAD }
4,906 test/mocha.js
4,906 additions, 0 deletions not shown
17 test/tests.html
@@ -0,0 +1,17 @@
+<html>
+ <head>
+ <title>Mocha</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <link rel="stylesheet" href="mocha.css" />
+ </head>
+ <body>
+ <div id="mocha"></div>
+ <script src="mocha.js"></script>
+ <script>mocha.setup('bdd')</script>
+ <script src="/build/build.js"></script>
+ <script src="to-array.js"></script>
+ <script>
+ mocha.run();
+ </script>
+ </body>
+</html>
69 test/to-array.js
@@ -0,0 +1,69 @@
+var toArray = require('to-array')
+var assert = require('timoxley-assert')
+var domify = require('component-domify')
+
+describe('identifying non-collections', function() {
+ it('correctly identifies JS primitives as non-collections', function () {
+ assert.deepEqual([], toArray(function(){})); // functions should fail
+ assert.deepEqual([], toArray(12345));
+ assert.deepEqual([], toArray(null));
+ assert.deepEqual([], toArray(undefined));
+ assert.deepEqual([], toArray(/regexp/));
+ assert.deepEqual([], toArray(new Date()));
+ })
+
+ it('detects window is not a collection', function() {
+ assert.deepEqual([], toArray(window));
+ })
+})
+
+describe('identifying collections', function() {
+ it('correctly identifies arrays', function() {
+ assert.deepEqual(['s','t','r','i','n','g'], toArray('string'));
+ assert.deepEqual([1, 2], toArray([1, 2]));
+ assert.deepEqual([], toArray([]));
+ })
+
+ it('correctly identifies arguments objects', function() {
+ (function() {
+ assert.deepEqual([1, 2], toArray(arguments), 'arguments should be arraylike'); // arguments collection
+ })(1, 2)
+ })
+
+ it('won\'t grab numeric indicies if they do not exist', function() {
+ var A = function() {}
+ A.prototype.length = 4
+ assert.deepEqual([], toArray(new A));
+ })
+})
+
+
+describe('testing against dom', function() {
+ var blink1, blink2
+ before(function() {
+ // pick elements that aren't likely to actually appear on the test page
+ blink1 = domify('<blink class="testblink"></blink>')
+ blink2 = domify('<blink class="testblink"></blink>')
+
+ document.body.appendChild(blink1)
+ document.body.appendChild(blink2)
+ })
+
+ after(function() {
+ document.body.removeChild(blink1)
+ document.body.removeChild(blink2)
+ })
+
+ it('fails for single nodes', function() {
+ assert.deepEqual([], toArray(blink1)); // single nodes should fail
+ })
+
+ it('detects htmlelement collections are array-like', function() {
+ assert.deepEqual([blink1, blink2], toArray(document.getElementsByTagName('blink')));
+ })
+
+ it('specifically identifies nodelists as a special collection', function() {
+ assert.deepEqual([blink1, blink2], toArray(document.querySelectorAll('.testblink'))); // NodeList
+ })
+})
+
Please sign in to comment.
Something went wrong with that request. Please try again.