Skip to content

Commit 7f35933

Browse files
first commit
0 parents  commit 7f35933

9 files changed

+246
-0
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extends: "@gianlucaguarini/eslint-config"
2+
env:
3+
mocha: true

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
# generated files
40+
index.js

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: node_js
2+
node_js:
3+
- "6.*"
4+
5+
branches:
6+
only:
7+
- master
8+
9+
before_install:
10+
11+
12+
- npm i @gianlucaguarini/[email protected]
13+
14+
- npm i
15+
- npm run build
16+
17+
notifications:
18+
email: false
19+
20+
sudo: false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Gianluca Guarini
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# bianco.dom-to-array
2+
3+
[![Build Status][travis-image]][travis-url]
4+
5+
[![NPM version][npm-version-image]][npm-url]
6+
[![NPM downloads][npm-downloads-image]][npm-url]
7+
[![MIT License][license-image]][license-url]
8+
9+
10+
11+
## Usage
12+
13+
```js
14+
import domToArray from 'bianco.dom-to-array'
15+
16+
var div = document.createElement('div')
17+
div.innerHTML = `
18+
<ul>
19+
<li>one</li>
20+
<li>two</li>
21+
</ul>
22+
`
23+
body.appendChild(div)
24+
25+
// It can convert node list
26+
const lis = document.querySelectorAll('li')
27+
const $lis = domToArray(lis)
28+
$lis.length // => 2
29+
typeof $lis // => 'object'
30+
31+
// It can convert a single node
32+
const li = document.querySelector('li')
33+
const $li = domToArray(li)
34+
$li.length // => 1
35+
typeof $li // => 'object'
36+
37+
```
38+
39+
## API
40+
41+
- `domToArray` Converts any DOM node/s into a loopable array like object
42+
43+
44+
[travis-image]:https://img.shields.io/travis/biancojs/dom-to-array.svg?style=flat-square
45+
[travis-url]:https://travis-ci.org/biancojs/dom-to-array
46+
47+
[license-image]:http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
48+
[license-url]:LICENSE.txt
49+
50+
[npm-version-image]:http://img.shields.io/npm/v/bianco.dom-to-array.svg?style=flat-square
51+
[npm-downloads-image]:http://img.shields.io/npm/dm/bianco.dom-to-array.svg?style=flat-square
52+
[npm-url]:https://npmjs.org/package/bianco.dom-to-array

index.next.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import isIterable from 'bianco.is-iterable'
2+
3+
/**
4+
* Converts any DOM node/s into a loopable array like object
5+
* @param { HTMLElement|NodeList } els - single html element or a node list
6+
* @returns { Object } always a loopable object
7+
*/
8+
export default function domToArray(els) {
9+
// can this object be already looped?
10+
if (!isIterable(els)) {
11+
// is it a node list?
12+
if (els.length)
13+
return Array.from(els)
14+
else
15+
// if it's a single node
16+
// it will be returned as "array" with one single entry
17+
return Array.from({ length: 1 }, _ => els)
18+
} else
19+
// this object could be looped out of the box
20+
return els
21+
}

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "bianco.dom-to-array",
3+
"version": "0.0.0",
4+
"description": "Covert DOM node/s into an array like object",
5+
"main": "index.js",
6+
"jsnext:main": "index.next.js",
7+
"scripts": {
8+
"prepublish": "npm run build && npm test",
9+
"lint": "eslint index.next.js test.js rollup.config.js",
10+
"build": "rollup -c",
11+
"test": "npm run lint && mocha test.js"
12+
},
13+
"files": [
14+
"index.js",
15+
"index.next.js"
16+
],
17+
"repository": {
18+
"type": "git",
19+
"url": "git+https://github.com/biancojs/dom-to-array.git"
20+
},
21+
"keywords": [
22+
"es6",
23+
"es2015",
24+
"dom",
25+
"array",
26+
"iterator",
27+
"dom loops",
28+
"nodes"
29+
],
30+
"author": "Gianluca Guarini <[email protected]> (http://gianlucaguarini.com)",
31+
"license": "MIT",
32+
"bugs": {
33+
"url": "https://github.com/biancojs/dom-to-array/issues"
34+
},
35+
"homepage": "https://github.com/biancojs/dom-to-array#readme",
36+
"devDependencies": {
37+
"jsdom": "9.5.0",
38+
"jsdom-global": "2.1.0",
39+
"bianco.is-iterable": "^0.0.1",
40+
"rollup-plugin-node-resolve": "^2.0.0"
41+
}
42+
}

rollup.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import resolve from 'rollup-plugin-node-resolve'
2+
3+
export default {
4+
entry: 'index.next.js',
5+
plugins: [
6+
resolve({
7+
jsnext: true
8+
})
9+
],
10+
targets: [
11+
{
12+
dest: 'index.js',
13+
format: 'cjs'
14+
}
15+
]
16+
}

test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require('jsdom-global')()
2+
const assert = require('assert')
3+
const domToArray = require('./')
4+
const body = document.body
5+
6+
describe('Bianco dom-to-array', function() {
7+
beforeEach(function () {
8+
var div = document.createElement('div')
9+
div.innerHTML = `
10+
<ul>
11+
<li>one</li>
12+
<li>two</li>
13+
</ul>
14+
`
15+
body.appendChild(div)
16+
})
17+
18+
it('It can convert node list', function() {
19+
const lis = document.querySelectorAll('li')
20+
const $lis = domToArray(lis)
21+
assert.equal($lis.length, 2)
22+
assert.equal(typeof $lis, 'object')
23+
})
24+
25+
it('It can convert a single node', function() {
26+
const li = document.querySelector('li')
27+
const $li = domToArray(li)
28+
assert.equal($li.length, 1)
29+
assert.equal(typeof $li, 'object')
30+
})
31+
})

0 commit comments

Comments
 (0)