From 124bccb3dcd29de5b99c38cb48457e08cd94df75 Mon Sep 17 00:00:00 2001 From: yuanchuan Date: Sun, 21 May 2017 12:55:17 +0800 Subject: [PATCH] fix odd() and even() calc --- Makefile | 2 +- css-doodle.js | 4 ++-- css-doodle.min.js | 2 +- src/cond.js | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 1e9d2af..77bd2fe 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ build: $(LIB) minify: $(TARGET) @npm run minify @./tools/trim - @cp $(TARGET_MIN) docs/components/ + @cp $(TARGET_MIN) docs/lib/ $(LIB): @npm install diff --git a/css-doodle.js b/css-doodle.js index 829a2ab..6eeb7e9 100644 --- a/css-doodle.js +++ b/css-doodle.js @@ -26,11 +26,11 @@ function col(x, y) { } function even(x, y, count) { - return _ => is.even(count); + return _ => is.even(count - 1); } function odd(x, y, count) { - return _ => is.odd(count); + return _ => is.odd(count - 1); } diff --git a/css-doodle.min.js b/css-doodle.min.js index fe57c43..5493d25 100644 --- a/css-doodle.min.js +++ b/css-doodle.min.js @@ -1 +1 @@ -!function(e,t){typeof exports==='object'&&typeof module!=='undefined'?t():typeof define==='function'&&define.amd?define(t):(t())}(this,(function(){"use strict";var e={even:e=>!!(e%2),odd:e=>!(e%2)};function t(e,t,n){return e=>e==n}function n(e,t){return(n,r)=>e==n&&t==r}function r(t){return n=>/^(even|odd)$/.test(n)?e[n](t):n==t}function u(t,n){return t=>/^(even|odd)$/.test(t)?e[t](n):t==n}function s(t,n,r){return t=>e.even(r)}function c(t,n,r){return t=>e.odd(r)}var o=Object.freeze({nth:t,at:n,row:r,col:u,even:s,odd:c});function l(e,t,n){return e=>n}function i(e){return t=>e}function a(e,t){return e=>t}function p(){return function(...e){return h.apply(null,e)}}function f(){return function(...e){return h(d(m(y)).apply(null,e))}}function h(...e){let t=e.reduce((e,t)=>e.concat(t),[]);return t[Math.floor(Math.random()*t.length)]}function d(e){return function(...t){let n={},r=t.join('-');if(n[r])return n[r];return n[r]=e.apply(null,t)}}function y(e,t,n){let r=0,u=e=>e>0&&e<1?.1:1,s=arguments.length;s==1&&([e,t]=[u(e),e]);s<3&&(n=u(t));let c=[];while(n>0&&et){c.push(e);e+=n;if(r++>=1e3)break}return c}function m(e){return function(...t){let n=_(t[0]);if(n){t=t.map(w);return x(e,n).apply(null,t)}return e.apply(null,t)}}function x(e,t){return function(...n){n=n.map(w);let r=e.apply(null,n);t&&(r=r.map(e=>e+t));return r}}function _(e){if(!e)return'';let t=/(%|cm|em|ex|in|mm|pc|pt|px|vh|vw|vmin|deg|ms|s)$/,n=''.trim.call(e).match(t);return n?n[0]:''}function w(e){let t=_(e);return t?+(e.replace(t,'')):e}var g=Object.freeze({index:l,row:i,col:a,any:p,rand:f});function b(e){if(Array.isArray(e))return e;return Object.keys(e).map(t=>e[t])}function z(e,...t){return t.reduce((e,t)=>e.apply(null,b(t)),e)}function v(e){return(e||[]).join('\n')}function k(e){return Array.isArray(e)?e:[e]}function T(e,t,n){return Math.max(t,Math.min(n,e))}class H{constructor(e){this.tokens=e;this.rules={};this.props={};this.styles={host:'',cells:''}}add_rule(e,t){var n=this.rules[e];n||(n=this.rules[e]=[]);n.push.apply(n,k(t))}compose_selector(e,t = ''){return`.cell:nth-of-type(${e}) .shape${t}`}compose_value(e,t){return e.reduce((e,n)=>{switch(n.type){case 'text':{e+=n.value;break};case 'function':{var r=g[n.name.substr(1)];r&&(e+=z(r,t,n.arguments))}}return e},'')}compose_rule(e,t){var n=e.property,r=this.compose_value(e.value,t);n=='transition'&&(this.props.has_transition=!0);return`${n}:${r};`}compose(e,t){(t||this.tokens).forEach((t,n)=>{if(t.skip)return!1;switch(t.type){case 'rule':this.add_rule(this.compose_selector(e.count),this.compose_rule(t,e));break;case 'psudo':{t.selector.startsWith(':doodle')&&(t.selector=t.selector.replace(/^\:+doodle/,':host'));var s=t.selector.startsWith(':host');s&&(t.skip=!0);var c=t.styles.map(t=>this.compose_rule(t,e)),l=s?t.selector:this.compose_selector(e.count,t.selector);this.add_rule(l,c);break};case 'cond':var u=o[t.name.substr(1)];if(u){var r=z(u,e,t.arguments);r&&this.compose(e,t.styles)};break}})}output(){Object.keys(this.rules).forEach(e=>{var t=e.startsWith(':host');let n=t?'host':'cells';this.styles[n]+=`${e}{${v(this.rules[e])}}`});return{props:this.props,styles:this.styles}}}function L(e,t){var n=new H(e);for(var r=1,u=0;r<=t.x;++r){for(var s=1;s<=t.y;++s)n.compose({x:r,y:s,count:++u})}return n.output()}let M={func:()=>({type:'function',name:'',arguments:[]}),text:()=>({type:'text',value:''}),psudo:()=>({type:'psudo',selector:'',styles:[]}),cond:()=>({type:'cond',name:'',styles:[],arguments:[]}),rule:()=>({type:'rule',property:'',value:[]}),comment:()=>({type:'comment',value:''})},S={'(':')','[':']','{':'}'},A={white_space(e){return /[\s\n\t]/.test(e)},open_bracket(e){return S.hasOwnProperty(e)},close_bracket_of(e){var t=S[e];return e=>e==t},number(e){return!isNaN(e)}};function W(e){var t=0,n=1,r=1;return{curr:(n = 0)=>e[t+n],end:()=>e.length<=t,info:()=>({index:t,col:n,line:r}),next:()=>{var u=e[t++];u=='\n'?(r++, n=0):n++;return u}}}function j(e,{col:t,line:n}){throw new Error(`(at line ${n}, column ${t}) ${e}`)}function C(e){var[t,n]=[e.curr(),e.curr()],r=A.close_bracket_of(n);e.next();while(!e.end()){if(r(n=e.curr())){t+=n;break}else A.open_bracket(n)?(t+=C(e)):(t+=n);e.next()}return t}function q(e,t ={}){var n=M.comment(),r=e.curr();r!='#'&&e.next();e.next();while(!e.end()){if((r=e.curr())=='*'&&e.curr(1)=='/')break;else n.value+=r;r=e.curr();if(t.inline){if(r=='\n')return n}else{if(r=='*'&&e.curr(1)=='/')break}n.value+=r;e.next()}e.next();e.next();return n}function B(e){var t='',n;while(!e.end()){if((n=e.curr())==':')break;else /[a-zA-Z\-]/.test(n)?A.white_space(n)||(t+=n):j('Syntax error:Bad property name.',e.info());e.next()}return t}function E(e,t){var n='',r;e.next();while(!e.end()){if((r=e.curr())==t){if(e.curr(-1)!=='\\')break;else n+=r}else n+=r;e.next()}return n}function O(e){var t=[],n='',r;while(!e.end()){if(A.open_bracket(r=e.curr()))n+=C(e);else if(/['"]/.test(r))n+=E(e,r);else if(/[,)]/.test(r)){n.length&&(t.push(A.number(+n)?+n:n),n='');if(r==')')break}else A.white_space(r)||(n+=r);e.next()}return t}function P(e){var t=M.func(),n='',r;while(!e.end()){if((r=e.curr())==')')break;if(r=='('){e.next();t.name=n;t.arguments=O(e);break}else n+=r;e.next()}return t}function R(e){var t=M.text(),n;let r=[];while(!e.end()){if((n=e.curr())=='\n'){e.next();continue}else if(/[;}]/.test(n)){t.value.length&&r.push(t);t=M.text();break}else n=='@'?(t.value.length&&r.push(t),t=M.text(),r.push(P(e))):(!A.white_space(n)||!A.white_space(e.curr(-1)))&&(n==':'&&j('Syntax error:Bad property name.',e.info()),t.value+=n);e.next()}t.value.length&&r.push(t);return r}function D(e){var t='',n;while(!e.end()){if((n=e.curr())=='{')break;else A.white_space(n)||(t+=n);e.next()}return t}function F(e){var t={name:'',arguments:[]},n;while(!e.end()){if((n=e.curr())=='(')e.next(),t.arguments=O(e);else if(/[){]/.test(n))break;else A.white_space(n)||(t.name+=n);e.next()}return t}function G(e){var t=M.psudo(),n;while(!e.end()){if((n=e.curr())=='}')break;if(A.white_space(n)){e.next();continue}else t.selector?t.styles.push(I(e)):(t.selector=D(e));e.next()}return t}function I(e){var t=M.rule(),n;while(!e.end()){if((n=e.curr())==';')break;else if(!t.property.length)t.property=B(e);else{t.value=R(e);break}e.next()}return t}function J(e){var t=M.cond(),n;while(!e.end()){if((n=e.curr())=='}')break;else if(!t.name.length)Object.assign(t,F(e));else if(n==':'){var r=G(e);r.selector&&t.styles.push(r)}else if(n=='@')t.styles.push(J(e));else if(!A.white_space(n)){var u=I(e);u.property&&t.styles.push(u)}e.next()}return t}function K(e){let t=W(e),n=[];while(!t.end()){var r=t.curr();if(A.white_space(r)){t.next();continue}else if(r=='/'&&t.curr(1)=='*')n.push(q(t));else if(r=='#'||r=='/'&&t.curr(1)=='/')n.push(q(t,{inline:!0}));else if(r==':'){var u=G(t);u.selector&&n.push(u)}else if(r=='@'){var s=J(t);s.name.length&&n.push(s)}else if(!A.white_space(r)){var c=I(t);c.property&&n.push(c)}t.next()}return n}function N(e,t){return L(K(e),t)}let Q=1,U=16;function V(e){let[t,n]=(e+'').replace(/\s+/g,'').replace(/[,,xX]+/,'x').split('x').map(Number),r={x:T(t||Q,1,U),y:T(n||t||Q,1,U)};return Object.assign({},r,{count:r.x*r.y})}let X="\n:host{display:block;visibility:visible;width:1em;height:1em;will-change:transform\n}.container{position:relative;width:100%;height:100%\n}.container:after{content:'';display:block;clear:both;visibility:hidden\n}.cell{position:relative;float:left;line-height:0;box-sizing:border-box\n}.shape{box-sizing:border-box;line-height:0;position:absolute;width:100%;height:100%;transform-origin:center center;z-index:1;display:flex;align-items:center;justify-content:center\n}";class Y extends HTMLElement{constructor(){super();this.doodle=this.attachShadow({mode:'open'})}connectedCallback(){setTimeout(()=>{if(!this.innerHTML.trim()){return!1}let e;try{e=N(this.innerHTML,this.size=V(this.getAttribute('grid')))}catch(e){this.innerHTML='';throw new Error(e)};let{has_transition:t}=e.props;this.doodle.innerHTML=`
${this.html_cells()}
`;t&&setTimeout(()=>{this.set_style('.style-cells',e.styles.cells)},50)})}style_size(){return`.cell{width:${100/this.size.x+'%'};height:${100/this.size.y+'%'};}`}html_cells(){let e="\n
";return e.repeat(this.size.count)}set_style(e,t){let n=this.shadowRoot.querySelector(e);n&&(n.styleSheet?(n.styleSheet.cssText=t):(n.innerHTML=t))}update(e){if(!e){return!1}this.size||(this.size=V(this.getAttribute('grid')));let t=N(e,this.size);this.set_style('.style-container',this.style_size()+t.styles.host);this.set_style('.style-cells',t.styles.cells);this.innerHTML=e}refresh(){this.update(this.innerHTML)}get grid(){return Object.assign({},this.size)}set grid(e){this.setAttribute('grid',e);this.connectedCallback()}static get observedAttributes(){return['grid']}attributeChangedCallback(e,t,n){e=='grid'&&t&&(t!==n&&(this.grid=n))}}customElements.define('css-doodle',Y)})) \ No newline at end of file +!function(e,t){typeof exports==='object'&&typeof module!=='undefined'?t():typeof define==='function'&&define.amd?define(t):(t())}(this,(function(){"use strict";var e={even:e=>!!(e%2),odd:e=>!(e%2)};function t(e,t,n){return e=>e==n}function n(e,t){return(n,r)=>e==n&&t==r}function r(t){return n=>/^(even|odd)$/.test(n)?e[n](t):n==t}function u(t,n){return t=>/^(even|odd)$/.test(t)?e[t](n):t==n}function s(t,n,r){return t=>e.even(r-1)}function c(t,n,r){return t=>e.odd(r-1)}var o=Object.freeze({nth:t,at:n,row:r,col:u,even:s,odd:c});function l(e,t,n){return e=>n}function i(e){return t=>e}function a(e,t){return e=>t}function p(){return function(...e){return h.apply(null,e)}}function f(){return function(...e){return h(d(m(y)).apply(null,e))}}function h(...e){let t=e.reduce((e,t)=>e.concat(t),[]);return t[Math.floor(Math.random()*t.length)]}function d(e){return function(...t){let n={},r=t.join('-');if(n[r])return n[r];return n[r]=e.apply(null,t)}}function y(e,t,n){let r=0,u=e=>e>0&&e<1?.1:1,s=arguments.length;s==1&&([e,t]=[u(e),e]);s<3&&(n=u(t));let c=[];while(n>0&&et){c.push(e);e+=n;if(r++>=1e3)break}return c}function m(e){return function(...t){let n=_(t[0]);if(n){t=t.map(w);return x(e,n).apply(null,t)}return e.apply(null,t)}}function x(e,t){return function(...n){n=n.map(w);let r=e.apply(null,n);t&&(r=r.map(e=>e+t));return r}}function _(e){if(!e)return'';let t=/(%|cm|em|ex|in|mm|pc|pt|px|vh|vw|vmin|deg|ms|s)$/,n=''.trim.call(e).match(t);return n?n[0]:''}function w(e){let t=_(e);return t?+(e.replace(t,'')):e}var g=Object.freeze({index:l,row:i,col:a,any:p,rand:f});function b(e){if(Array.isArray(e))return e;return Object.keys(e).map(t=>e[t])}function z(e,...t){return t.reduce((e,t)=>e.apply(null,b(t)),e)}function v(e){return(e||[]).join('\n')}function k(e){return Array.isArray(e)?e:[e]}function T(e,t,n){return Math.max(t,Math.min(n,e))}class H{constructor(e){this.tokens=e;this.rules={};this.props={};this.styles={host:'',cells:''}}add_rule(e,t){var n=this.rules[e];n||(n=this.rules[e]=[]);n.push.apply(n,k(t))}compose_selector(e,t = ''){return`.cell:nth-of-type(${e}) .shape${t}`}compose_value(e,t){return e.reduce((e,n)=>{switch(n.type){case 'text':{e+=n.value;break};case 'function':{var r=g[n.name.substr(1)];r&&(e+=z(r,t,n.arguments))}}return e},'')}compose_rule(e,t){var n=e.property,r=this.compose_value(e.value,t);n=='transition'&&(this.props.has_transition=!0);return`${n}:${r};`}compose(e,t){(t||this.tokens).forEach((t,n)=>{if(t.skip)return!1;switch(t.type){case 'rule':this.add_rule(this.compose_selector(e.count),this.compose_rule(t,e));break;case 'psudo':{t.selector.startsWith(':doodle')&&(t.selector=t.selector.replace(/^\:+doodle/,':host'));var s=t.selector.startsWith(':host');s&&(t.skip=!0);var c=t.styles.map(t=>this.compose_rule(t,e)),l=s?t.selector:this.compose_selector(e.count,t.selector);this.add_rule(l,c);break};case 'cond':var u=o[t.name.substr(1)];if(u){var r=z(u,e,t.arguments);r&&this.compose(e,t.styles)};break}})}output(){Object.keys(this.rules).forEach(e=>{var t=e.startsWith(':host');let n=t?'host':'cells';this.styles[n]+=`${e}{${v(this.rules[e])}}`});return{props:this.props,styles:this.styles}}}function L(e,t){var n=new H(e);for(var r=1,u=0;r<=t.x;++r){for(var s=1;s<=t.y;++s)n.compose({x:r,y:s,count:++u})}return n.output()}let M={func:()=>({type:'function',name:'',arguments:[]}),text:()=>({type:'text',value:''}),psudo:()=>({type:'psudo',selector:'',styles:[]}),cond:()=>({type:'cond',name:'',styles:[],arguments:[]}),rule:()=>({type:'rule',property:'',value:[]}),comment:()=>({type:'comment',value:''})},S={'(':')','[':']','{':'}'},A={white_space(e){return /[\s\n\t]/.test(e)},open_bracket(e){return S.hasOwnProperty(e)},close_bracket_of(e){var t=S[e];return e=>e==t},number(e){return!isNaN(e)}};function W(e){var t=0,n=1,r=1;return{curr:(n = 0)=>e[t+n],end:()=>e.length<=t,info:()=>({index:t,col:n,line:r}),next:()=>{var u=e[t++];u=='\n'?(r++, n=0):n++;return u}}}function j(e,{col:t,line:n}){throw new Error(`(at line ${n}, column ${t}) ${e}`)}function C(e){var[t,n]=[e.curr(),e.curr()],r=A.close_bracket_of(n);e.next();while(!e.end()){if(r(n=e.curr())){t+=n;break}else A.open_bracket(n)?(t+=C(e)):(t+=n);e.next()}return t}function q(e,t ={}){var n=M.comment(),r=e.curr();r!='#'&&e.next();e.next();while(!e.end()){if((r=e.curr())=='*'&&e.curr(1)=='/')break;else n.value+=r;r=e.curr();if(t.inline){if(r=='\n')return n}else{if(r=='*'&&e.curr(1)=='/')break}n.value+=r;e.next()}e.next();e.next();return n}function B(e){var t='',n;while(!e.end()){if((n=e.curr())==':')break;else /[a-zA-Z\-]/.test(n)?A.white_space(n)||(t+=n):j('Syntax error:Bad property name.',e.info());e.next()}return t}function E(e,t){var n='',r;e.next();while(!e.end()){if((r=e.curr())==t){if(e.curr(-1)!=='\\')break;else n+=r}else n+=r;e.next()}return n}function O(e){var t=[],n='',r;while(!e.end()){if(A.open_bracket(r=e.curr()))n+=C(e);else if(/['"]/.test(r))n+=E(e,r);else if(/[,)]/.test(r)){n.length&&(t.push(A.number(+n)?+n:n),n='');if(r==')')break}else A.white_space(r)||(n+=r);e.next()}return t}function P(e){var t=M.func(),n='',r;while(!e.end()){if((r=e.curr())==')')break;if(r=='('){e.next();t.name=n;t.arguments=O(e);break}else n+=r;e.next()}return t}function R(e){var t=M.text(),n;let r=[];while(!e.end()){if((n=e.curr())=='\n'){e.next();continue}else if(/[;}]/.test(n)){t.value.length&&r.push(t);t=M.text();break}else n=='@'?(t.value.length&&r.push(t),t=M.text(),r.push(P(e))):(!A.white_space(n)||!A.white_space(e.curr(-1)))&&(n==':'&&j('Syntax error:Bad property name.',e.info()),t.value+=n);e.next()}t.value.length&&r.push(t);return r}function D(e){var t='',n;while(!e.end()){if((n=e.curr())=='{')break;else A.white_space(n)||(t+=n);e.next()}return t}function F(e){var t={name:'',arguments:[]},n;while(!e.end()){if((n=e.curr())=='(')e.next(),t.arguments=O(e);else if(/[){]/.test(n))break;else A.white_space(n)||(t.name+=n);e.next()}return t}function G(e){var t=M.psudo(),n;while(!e.end()){if((n=e.curr())=='}')break;if(A.white_space(n)){e.next();continue}else t.selector?t.styles.push(I(e)):(t.selector=D(e));e.next()}return t}function I(e){var t=M.rule(),n;while(!e.end()){if((n=e.curr())==';')break;else if(!t.property.length)t.property=B(e);else{t.value=R(e);break}e.next()}return t}function J(e){var t=M.cond(),n;while(!e.end()){if((n=e.curr())=='}')break;else if(!t.name.length)Object.assign(t,F(e));else if(n==':'){var r=G(e);r.selector&&t.styles.push(r)}else if(n=='@')t.styles.push(J(e));else if(!A.white_space(n)){var u=I(e);u.property&&t.styles.push(u)}e.next()}return t}function K(e){let t=W(e),n=[];while(!t.end()){var r=t.curr();if(A.white_space(r)){t.next();continue}else if(r=='/'&&t.curr(1)=='*')n.push(q(t));else if(r=='#'||r=='/'&&t.curr(1)=='/')n.push(q(t,{inline:!0}));else if(r==':'){var u=G(t);u.selector&&n.push(u)}else if(r=='@'){var s=J(t);s.name.length&&n.push(s)}else if(!A.white_space(r)){var c=I(t);c.property&&n.push(c)}t.next()}return n}function N(e,t){return L(K(e),t)}let Q=1,U=16;function V(e){let[t,n]=(e+'').replace(/\s+/g,'').replace(/[,,xX]+/,'x').split('x').map(Number),r={x:T(t||Q,1,U),y:T(n||t||Q,1,U)};return Object.assign({},r,{count:r.x*r.y})}let X="\n:host{display:block;visibility:visible;width:1em;height:1em;will-change:transform\n}.container{position:relative;width:100%;height:100%\n}.container:after{content:'';display:block;clear:both;visibility:hidden\n}.cell{position:relative;float:left;line-height:0;box-sizing:border-box\n}.shape{box-sizing:border-box;line-height:0;position:absolute;width:100%;height:100%;transform-origin:center center;z-index:1;display:flex;align-items:center;justify-content:center\n}";class Y extends HTMLElement{constructor(){super();this.doodle=this.attachShadow({mode:'open'})}connectedCallback(){setTimeout(()=>{if(!this.innerHTML.trim()){return!1}let e;try{e=N(this.innerHTML,this.size=V(this.getAttribute('grid')))}catch(e){this.innerHTML='';throw new Error(e)};let{has_transition:t}=e.props;this.doodle.innerHTML=`
${this.html_cells()}
`;t&&setTimeout(()=>{this.set_style('.style-cells',e.styles.cells)},50)})}style_size(){return`.cell{width:${100/this.size.x+'%'};height:${100/this.size.y+'%'};}`}html_cells(){let e="\n
";return e.repeat(this.size.count)}set_style(e,t){let n=this.shadowRoot.querySelector(e);n&&(n.styleSheet?(n.styleSheet.cssText=t):(n.innerHTML=t))}update(e){if(!e){return!1}this.size||(this.size=V(this.getAttribute('grid')));let t=N(e,this.size);this.set_style('.style-container',this.style_size()+t.styles.host);this.set_style('.style-cells',t.styles.cells);this.innerHTML=e}refresh(){this.update(this.innerHTML)}get grid(){return Object.assign({},this.size)}set grid(e){this.setAttribute('grid',e);this.connectedCallback()}static get observedAttributes(){return['grid']}attributeChangedCallback(e,t,n){e=='grid'&&t&&(t!==n&&(this.grid=n))}}customElements.define('css-doodle',Y)})) \ No newline at end of file diff --git a/src/cond.js b/src/cond.js index 383840a..c497cd5 100644 --- a/src/cond.js +++ b/src/cond.js @@ -20,9 +20,9 @@ export function col(x, y) { } export function even(x, y, count) { - return _ => is.even(count); + return _ => is.even(count - 1); } export function odd(x, y, count) { - return _ => is.odd(count); + return _ => is.odd(count - 1); }