Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 29, 2018
1 parent a7c7e93 commit e69ac92
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 142 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
svg-element-attributes.js
svg-element-attributes.min.js
197 changes: 98 additions & 99 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,106 @@
'use strict';
'use strict'

var fs = require('fs');
var https = require('https');
var bail = require('bail');
var concat = require('concat-stream');
var alphaSort = require('alpha-sort');
var unified = require('unified');
var parse = require('rehype-parse');
var q = require('hast-util-select');
var toString = require('hast-util-to-string');
var ev = require('hast-util-is-event-handler');
var fs = require('fs')
var https = require('https')
var bail = require('bail')
var concat = require('concat-stream')
var alphaSort = require('alpha-sort')
var unified = require('unified')
var parse = require('rehype-parse')
var q = require('hast-util-select')
var toString = require('hast-util-to-string')
var ev = require('hast-util-is-event-handler')

var actual = 0;
var expected = 3;
var proc = unified().use(parse)

var all = {};
var actual = 0
var expected = 3

/* Crawl SVG 1.1. */
https.get('https://www.w3.org/TR/SVG/attindex.html', function (res) {
res.pipe(concat(onconcat)).on('error', bail);
var all = {}

https.get('https://www.w3.org/TR/SVG/attindex.html', onsvg1)
https.get('https://www.w3.org/TR/SVGTiny12/attributeTable.html', ontiny)
https.get('https://www.w3.org/TR/SVG2/attindex.html', onsvg2)

function onsvg1(res) {
res.pipe(concat(onconcat)).on('error', bail)

function onconcat(buf) {
var tree = unified().use(parse).parse(buf);
var map = {};
var tree = proc.parse(buf)
var map = {}

q.selectAll('.property-table tr', tree).forEach(each);
q.selectAll('.property-table tr', tree).forEach(each)

done(map);
done(map)

function each(node) {
var elements = q.selectAll('.element-name', node);
var elements = q.selectAll('.element-name', node)

q.selectAll('.attr-name', node).forEach(every);
q.selectAll('.attr-name', node).forEach(every)

function every(name) {
elements
.map(toString)
.map(clean)
.forEach(add(map, clean(toString(name))));
.forEach(add(map, clean(toString(name))))
}
}

function clean(value) {
return value.replace(/[‘’]/g, '');
return value.replace(/[‘’]/g, '')
}
}
});
}

/* Crawl SVG Tiny 1.2. */
https.get('https://www.w3.org/TR/SVGTiny12/attributeTable.html', function (res) {
res.pipe(concat(onconcat)).on('error', bail);
function ontiny(res) {
res.pipe(concat(onconcat)).on('error', bail)

function onconcat(buf) {
var tree = unified().use(parse).parse(buf);
var map = {};
var tree = proc.parse(buf)
var map = {}

q.selectAll('#attributes .attribute', tree).forEach(each);
q.selectAll('#attributes .attribute', tree).forEach(each)

done(map);
done(map)

function each(node) {
q
.selectAll('.element', node)
.map(toString)
.forEach(add(map, toString(q.select('.attribute-name', node))));
.forEach(add(map, toString(q.select('.attribute-name', node))))
}
}
});
}

/* Crawl SVG 2. */
https.get('https://www.w3.org/TR/SVG2/attindex.html', function (res) {
res.pipe(concat(onconcat)).on('error', bail);
function onsvg2(res) {
res.pipe(concat(onconcat)).on('error', bail)

function onconcat(buf) {
var tree = unified().use(parse).parse(buf);
var map = {};
var tree = proc.parse(buf)
var map = {}

q.selectAll('tbody tr', tree).forEach(each);
q.selectAll('tbody tr', tree).forEach(each)

done(map);
done(map)

function each(node) {
q
.selectAll('.element-name span', node)
.map(toString)
.forEach(add(map, toString(q.select('.attr-name span', node))));
.forEach(add(map, toString(q.select('.attr-name span', node))))
}
}
});
}

// /* Add a map. */
/* Add a map. */
function done(map) {
merge(all, clean(map));
cleanAll(all);
merge(all, clean(map))
cleanAll(all)

actual++;
actual++

if (actual === expected) {
fs.writeFile('index.json', JSON.stringify(sort(all), 0, 2) + '\n', bail);
fs.writeFile('index.json', JSON.stringify(sort(all), 0, 2) + '\n', bail)
}
}

Expand All @@ -110,112 +113,108 @@ function add(map, name) {
name.slice(0, 4) === 'xml:' ||
name.slice(0, 6) === 'xlink:'
) {
return noop;
return noop
}

return fn;
return fn

function fn(tagName) {
var attributes = map[tagName] || (map[tagName] = []);
var attributes = map[tagName] || (map[tagName] = [])

if (attributes.indexOf(name) === -1) {
attributes.push(name);
attributes.push(name)
}
}

function noop() {}
}

function clean(map) {
var result = {};
var list = [];
var globals = [];
var result = {}
var list = []
var globals = []

/* Find all used attributes. */
Object.keys(map).forEach(function (tagName) {
map[tagName].forEach(function (attribute) {
Object.keys(map).forEach(function(tagName) {
map[tagName].forEach(function(attribute) {
if (list.indexOf(attribute) === -1) {
list.push(attribute);
list.push(attribute)
}
});
});
})
})

/* Find global attributes. */
list.forEach(function (attribute) {
var global = true;
var key;
list.forEach(function(attribute) {
var global = true
var key

for (key in map) {
if (map[key].indexOf(attribute) === -1) {
global = false;
break;
global = false
break
}
}

if (global) {
globals.push(attribute);
globals.push(attribute)
}
});
})

/* Remove globals. */
result = {
'*': globals
};
}

Object.keys(map)
.sort()
.forEach(function (tagName) {
.forEach(function(tagName) {
var attributes = map[tagName]
.filter(function (attribute) {
return globals.indexOf(attribute) === -1;
.filter(function(attribute) {
return globals.indexOf(attribute) === -1
})
.sort();
.sort()

if (attributes.length !== 0) {
result[tagName] = attributes;
result[tagName] = attributes
}
});
})

return result;
return result
}

function merge(left, right) {
Object.keys(right).forEach(each);
Object.keys(right).forEach(each)

function each(tagName) {
left[tagName] = (left[tagName] || [])
.concat(right[tagName])
.filter(function (attribute, index, list) {
return list.indexOf(attribute) === index;
.filter(function(attribute, index, list) {
return list.indexOf(attribute) === index
})
.sort();
.sort()
}
}

function cleanAll(map) {
var globals = map['*'];

Object
.keys(map)
.forEach(function (tagName) {
if (tagName !== '*') {
map[tagName] = map[tagName]
.filter(function (attribute) {
return globals.indexOf(attribute) === -1;
});
}
});
var globals = map['*']

Object.keys(map).forEach(function(tagName) {
if (tagName !== '*') {
map[tagName] = map[tagName].filter(function(attribute) {
return globals.indexOf(attribute) === -1
})
}
})
}

function sort(map) {
var result = {};
var result = {}

Object
.keys(map)
Object.keys(map)
.sort(alphaSort.asc)
.forEach(function (key) {
result[key] = map[key];
});
.forEach(function(key) {
result[key] = map[key]
})

return result;
return result
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"hast-util-is-event-handler": "^1.0.0",
"hast-util-select": "^1.0.1",
"hast-util-to-string": "^1.0.1",
"prettier": "^1.12.1",
"rehype-parse": "^4.0.0",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
Expand All @@ -40,17 +41,24 @@
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -qfo",
"build-crawl": "node build",
"generate": "node build",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.json --bare -s svgElementAttributes > svg-element-attributes.js",
"build-mangle": "esmangle svg-element-attributes.js > svg-element-attributes.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test": "npm run build && npm run lint && npm run test-api"
"test": "npm run format && npm run build && npm run test-api"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ npm install svg-element-attributes
## Usage

```javascript
var svgElementAttributes = require('svg-element-attributes');
var svgElementAttributes = require('svg-element-attributes')

console.log(svgElementAttributes['*']);
console.log(svgElementAttributes.circle);
console.log(svgElementAttributes['*'])
console.log(svgElementAttributes.circle)
```

Yields:
Expand Down
Loading

0 comments on commit e69ac92

Please sign in to comment.