Comparing version 0.0.2 to 1.0.0
{ | ||
"name": "xmltag", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "composable xml tag generator", | ||
@@ -33,6 +33,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"jasmine": "^2.8.0", | ||
"jasmine": "^3.4.0", | ||
"nodemon": "^1.12.1", | ||
"paratest": "0.0.4" | ||
"paratest": "^0.0.6" | ||
} | ||
} |
@@ -1,31 +0,50 @@ | ||
const paratest = require('paratest'); | ||
const tag = require('../src/xmltag'); | ||
const paratest = require('paratest') | ||
const tag = require('../src/xmltag') | ||
const svg = tag('svg'); | ||
const div = tag('div') | ||
const tests = [ | ||
{ | ||
subject: svg, | ||
subject: div, | ||
cases: [ | ||
{ | ||
name: "works", | ||
args: [{one: "two"}, 3], | ||
result: '<svg one="two">3</svg>' | ||
name: 'without attrs or children', | ||
args: [], | ||
result: '<div />' | ||
}, { | ||
name: "without attrs", | ||
args: [{}, "hello"], | ||
result: '<svg >hello</svg>' | ||
name: 'works with attrs and no children', | ||
args: [{ one: 'two' }], | ||
result: '<div one="two" />' | ||
}, { | ||
name: "nested", | ||
args: [{one: "two", three: "four"}, svg({five: "six"}, "hello")], | ||
result: '<svg one="two" three="four"><svg five="six">hello</svg></svg>' | ||
name: 'works with attrs and children', | ||
args: [{ one: 'two' }, 3], | ||
result: '<div one="two">3</div>' | ||
}, { | ||
name: "without children", | ||
args: [{one:"two"}], | ||
result: '<svg one="two"/>' | ||
name: 'nested', | ||
args: [ | ||
{ one: 'two', three: 'four' }, | ||
div({ five: 'six' }, 'hello') | ||
], | ||
result: '<div one="two" three="four"><div five="six">hello</div></div>' | ||
}, { | ||
name: 'works without attrs', | ||
args: ['hello'], | ||
result: '<div>hello</div>' | ||
}, { | ||
name: 'nested without attrs or children', | ||
args: [div()], | ||
result: '<div><div /></div>' | ||
}, { | ||
name: 'nested with children no attrs', | ||
args: [div('hello')], | ||
result: '<div><div>hello</div></div>' | ||
}, { | ||
name: 'without children', | ||
args: [{ one: 'two' }], | ||
result: '<div one="two" />' | ||
} | ||
] | ||
} | ||
]; | ||
] | ||
paratest("xmltest", tests); | ||
paratest('xmltest', tests) |
@@ -1,17 +0,27 @@ | ||
function spreadAttrs(attrs) { | ||
return Object.keys(attrs).map(key => { | ||
const str = `${key}="${attrs[key]}"`; | ||
return str; | ||
}).join(" "); | ||
const spreadAttrs = attrs => | ||
Object.keys(attrs).map(key => `${key}="${attrs[key]}"`).join(' ') | ||
const renderAttrs = attrs => attrs ? ` ${spreadAttrs(attrs)}` : '' | ||
const isObject = o => typeof o === 'object' && o != null | ||
const parseArgs = args => { | ||
const [firstArg, ...restArgs] = args | ||
const hasAttrs = isObject(firstArg) | ||
return { | ||
attrs: hasAttrs ? firstArg : undefined, | ||
children: hasAttrs ? restArgs : args | ||
} | ||
} | ||
function tag(name) { | ||
return function(attrs, ...children) { | ||
return `<${name} ${spreadAttrs(attrs)}` | ||
+ (children && children.length > 0 | ||
? `>${children.join("")}</${name}>` | ||
: '/>'); | ||
}; | ||
const tag = name => (...args) => { | ||
const { attrs, children } = parseArgs(args) | ||
const hasChildren = children.length > 0 | ||
return `<${name}${renderAttrs(attrs)}` + | ||
(hasChildren ? '' : ' /') + '>' + | ||
(hasChildren ? `${children.join('')}</${name}>` : '') | ||
} | ||
module.exports = tag; | ||
module.exports = tag |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4053
79
0
7