diff --git a/CHANGELOG.md b/CHANGELOG.md index 54434fa..532f50a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## 0.8.4 + +_2019-12-05_ + +Fixed bug with function helpers where empty string would be passed as first arg when no args were passed, causing default args not to work: + +```js +z.helper({ test: (x = 5) => `margin ${x}` }) + +z`test` // this would result in no style being applied, since x would be '' instead of defaulting to 5 +``` + ## 0.8.3 _2019-11-12_ diff --git a/package-lock.json b/package-lock.json index aa3d8be..94fce2e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "zaftig", - "version": "0.8.3", + "version": "0.8.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -941,9 +941,9 @@ "dev": true }, "terser": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.4.0.tgz", - "integrity": "sha512-oDG16n2WKm27JO8h4y/w3iqBGAOSCtq7k8dRmrn4Wf9NouL0b2WpMHGChFGZq4nFAQy1FsNJrVQHfurXOSTmOA==", + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.4.2.tgz", + "integrity": "sha512-Uufrsvhj9O1ikwgITGsZ5EZS6qPokUOkCegS7fYOdGTv+OA90vndUbU6PEjr5ePqHfNUbGyMO7xyIZv2MhsALQ==", "dev": true, "requires": { "commander": "^2.20.0", diff --git a/package.json b/package.json index 25ca93d..dc0fb84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zaftig", - "version": "0.8.3", + "version": "0.8.4", "description": "css for your js", "source": "src/index.js", "main": "dist/zaftig.es5.min.js", @@ -28,6 +28,6 @@ "buble": "^0.19.8", "jsdom": "^15.2.1", "ospec": "^4.0.1", - "terser": "^4.4.0" + "terser": "^4.4.2" } } diff --git a/src/index.js b/src/index.js index 43317b7..d768d10 100644 --- a/src/index.js +++ b/src/index.js @@ -223,7 +223,7 @@ const makeZ = (conf = {}) => { const runHelper = (key, value) => { const helper = helpers[key] return typeof helper === 'function' - ? helper(...(value || '').split(' ')) + ? helper(...(value ? value.split(' ') : [])) : helper && helper + ' ' + value } diff --git a/test.js b/test.js index 94011bb..d5944c7 100644 --- a/test.js +++ b/test.js @@ -21,8 +21,8 @@ const singleRule = (input, output, z = zaf.new({ id: 'test' })) => { o(z.getSheet().sheet.cssRules[0].cssText).equals('.test-1 {' + output + '}') } -const fullSheet = str => { - const z = zaf.new({ id: 'test', debug: true }) +const fullSheet = (str, conf) => { + const z = zaf.new({ ...conf, id: 'test', debug: true }) const [input, output] = str.split('===') const style = z(input) o('' + style).equals('.test-1') @@ -165,5 +165,51 @@ h 200 o(z.getSheet().sheet.cssRules.length).equals(2) }) o('flex does not generate px', () => o(zaf.style`flex 1`.trim()).equals('flex: 1;')) + o('helpers work', () => { + const helpers = { + 'no-arg': 'padding 10', + basic: 'margin', + 'basic-fn': (x = 1, y = 2, z = 3) => `content '${x} ${y} ${z}'`, + multi: 'height 50;width 50' + } + fullSheet( + css` +h1 { basic 5 } +h2 { basic 5 10 } +h3 { basic-fn; no-arg; multi } +h4 { basic-fn hello } +h5 { basic-fn hello world } +h6 { basic-fn hello world foo } +=== +.test-1 h1 { + margin: 5px; +} + +.test-1 h2 { + margin: 5px 10px; +} + +.test-1 h3 { + content: '1 2 3'; + padding: 10px; + height: 50px; + width: 50px; +} + +.test-1 h4 { + content: 'hello 2 3'; +} + +.test-1 h5 { + content: 'hello world 3'; +} + +.test-1 h6 { + content: 'hello world foo'; +} + `, + { helpers } + ) + }) // TODO: add tests for selector prefixing and better error handling (JSDOM doesn't seem to give syntax errors like browsers do) })