@csstools/postcss-trigonometric-functions 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changes to PostCSS Trigonometric Functions
2
+
3
+ ### 1.0.0 (Unreleased)
4
+
5
+ - Initial version
package/LICENSE.md ADDED
@@ -0,0 +1,108 @@
1
+ # CC0 1.0 Universal
2
+
3
+ ## Statement of Purpose
4
+
5
+ The laws of most jurisdictions throughout the world automatically confer
6
+ exclusive Copyright and Related Rights (defined below) upon the creator and
7
+ subsequent owner(s) (each and all, an “owner”) of an original work of
8
+ authorship and/or a database (each, a “Work”).
9
+
10
+ Certain owners wish to permanently relinquish those rights to a Work for the
11
+ purpose of contributing to a commons of creative, cultural and scientific works
12
+ (“Commons”) that the public can reliably and without fear of later claims of
13
+ infringement build upon, modify, incorporate in other works, reuse and
14
+ redistribute as freely as possible in any form whatsoever and for any purposes,
15
+ including without limitation commercial purposes. These owners may contribute
16
+ to the Commons to promote the ideal of a free culture and the further
17
+ production of creative, cultural and scientific works, or to gain reputation or
18
+ greater distribution for their Work in part through the use and efforts of
19
+ others.
20
+
21
+ For these and/or other purposes and motivations, and without any expectation of
22
+ additional consideration or compensation, the person associating CC0 with a
23
+ Work (the “Affirmer”), to the extent that he or she is an owner of Copyright
24
+ and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and
25
+ publicly distribute the Work under its terms, with knowledge of his or her
26
+ Copyright and Related Rights in the Work and the meaning and intended legal
27
+ effect of CC0 on those rights.
28
+
29
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
30
+ protected by copyright and related or neighboring rights (“Copyright and
31
+ Related Rights”). Copyright and Related Rights include, but are not limited
32
+ to, the following:
33
+ 1. the right to reproduce, adapt, distribute, perform, display, communicate,
34
+ and translate a Work;
35
+ 2. moral rights retained by the original author(s) and/or performer(s);
36
+ 3. publicity and privacy rights pertaining to a person’s image or likeness
37
+ depicted in a Work;
38
+ 4. rights protecting against unfair competition in regards to a Work,
39
+ subject to the limitations in paragraph 4(i), below;
40
+ 5. rights protecting the extraction, dissemination, use and reuse of data in
41
+ a Work;
42
+ 6. database rights (such as those arising under Directive 96/9/EC of the
43
+ European Parliament and of the Council of 11 March 1996 on the legal
44
+ protection of databases, and under any national implementation thereof,
45
+ including any amended or successor version of such directive); and
46
+ 7. other similar, equivalent or corresponding rights throughout the world
47
+ based on applicable law or treaty, and any national implementations
48
+ thereof.
49
+
50
+ 2. Waiver. To the greatest extent permitted by, but not in contravention of,
51
+ applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
52
+ unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright
53
+ and Related Rights and associated claims and causes of action, whether now
54
+ known or unknown (including existing as well as future claims and causes of
55
+ action), in the Work (i) in all territories worldwide, (ii) for the maximum
56
+ duration provided by applicable law or treaty (including future time
57
+ extensions), (iii) in any current or future medium and for any number of
58
+ copies, and (iv) for any purpose whatsoever, including without limitation
59
+ commercial, advertising or promotional purposes (the “Waiver”). Affirmer
60
+ makes the Waiver for the benefit of each member of the public at large and
61
+ to the detriment of Affirmer’s heirs and successors, fully intending that
62
+ such Waiver shall not be subject to revocation, rescission, cancellation,
63
+ termination, or any other legal or equitable action to disrupt the quiet
64
+ enjoyment of the Work by the public as contemplated by Affirmer’s express
65
+ Statement of Purpose.
66
+
67
+ 3. Public License Fallback. Should any part of the Waiver for any reason be
68
+ judged legally invalid or ineffective under applicable law, then the Waiver
69
+ shall be preserved to the maximum extent permitted taking into account
70
+ Affirmer’s express Statement of Purpose. In addition, to the extent the
71
+ Waiver is so judged Affirmer hereby grants to each affected person a
72
+ royalty-free, non transferable, non sublicensable, non exclusive,
73
+ irrevocable and unconditional license to exercise Affirmer’s Copyright and
74
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
75
+ maximum duration provided by applicable law or treaty (including future time
76
+ extensions), (iii) in any current or future medium and for any number of
77
+ copies, and (iv) for any purpose whatsoever, including without limitation
78
+ commercial, advertising or promotional purposes (the “License”). The License
79
+ shall be deemed effective as of the date CC0 was applied by Affirmer to the
80
+ Work. Should any part of the License for any reason be judged legally
81
+ invalid or ineffective under applicable law, such partial invalidity or
82
+ ineffectiveness shall not invalidate the remainder of the License, and in
83
+ such case Affirmer hereby affirms that he or she will not (i) exercise any
84
+ of his or her remaining Copyright and Related Rights in the Work or (ii)
85
+ assert any associated claims and causes of action with respect to the Work,
86
+ in either case contrary to Affirmer’s express Statement of Purpose.
87
+
88
+ 4. Limitations and Disclaimers.
89
+ 1. No trademark or patent rights held by Affirmer are waived, abandoned,
90
+ surrendered, licensed or otherwise affected by this document.
91
+ 2. Affirmer offers the Work as-is and makes no representations or warranties
92
+ of any kind concerning the Work, express, implied, statutory or
93
+ otherwise, including without limitation warranties of title,
94
+ merchantability, fitness for a particular purpose, non infringement, or
95
+ the absence of latent or other defects, accuracy, or the present or
96
+ absence of errors, whether or not discoverable, all to the greatest
97
+ extent permissible under applicable law.
98
+ 3. Affirmer disclaims responsibility for clearing rights of other persons
99
+ that may apply to the Work or any use thereof, including without
100
+ limitation any person’s Copyright and Related Rights in the Work.
101
+ Further, Affirmer disclaims responsibility for obtaining any necessary
102
+ consents, permissions or other rights required for any use of the Work.
103
+ 4. Affirmer understands and acknowledges that Creative Commons is not a
104
+ party to this document and has no duty or obligation with respect to this
105
+ CC0 or use of the Work.
106
+
107
+ For more information, please see
108
+ http://creativecommons.org/publicdomain/zero/1.0/.
package/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # PostCSS Trigonometric Functions [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
2
+
3
+ [<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-trigonometric-functions.svg" height="20">][npm-url]
4
+ [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/trigonometric-functions.svg" height="20">][css-url]
5
+ [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url]
6
+ [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
7
+
8
+ [PostCSS Trigonometric Functions] lets you use `sin`, `cos`, `tan`, `asin`, `acos`, `atan` and `atan2` to be able to compute trigonometric relationships following the [CSS Values 4] specification.
9
+
10
+ ```pcss
11
+ .trigonometry {
12
+ line-height: sin(pi / 4);
13
+ line-height: cos(.125turn);
14
+ line-height: tan(50grad);
15
+ transform: rotate(asin(-1));
16
+ transform: rotate(asin(sin(30deg + 1.0471967rad)));
17
+ transform: rotate(acos(-1));
18
+ transform: rotate(acos(cos(0 / 2 + 1 - 1)));
19
+ transform: rotate(atan(infinity));
20
+ transform: rotate(atan(e - 2.7182818284590452354));
21
+ transform: rotate(atan2(-infinity,-infinity));
22
+ transform: rotate(atan2(-infinity,infinity));
23
+ transform: rotate(atan2(-infinity,infinity));
24
+ transform: rotate(atan2(90, 15));
25
+ }
26
+
27
+ /* becomes */
28
+
29
+ .trigonometry {
30
+ line-height: 0.70711;
31
+ line-height: 0.70711;
32
+ line-height: 1;
33
+ transform: rotate(-90deg);
34
+ transform: rotate(90deg);
35
+ transform: rotate(180deg);
36
+ transform: rotate(0deg);
37
+ transform: rotate(90deg);
38
+ transform: rotate(0deg);
39
+ transform: rotate(-135deg);
40
+ transform: rotate(-45deg);
41
+ transform: rotate(-45deg);
42
+ transform: rotate(80.54deg);
43
+ }
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ Add [PostCSS Trigonometric Functions] to your project:
49
+
50
+ ```bash
51
+ npm install postcss @csstools/postcss-trigonometric-functions --save-dev
52
+ ```
53
+
54
+ Use it as a [PostCSS] plugin:
55
+
56
+ ```js
57
+ const postcss = require('postcss');
58
+ const postcssTrigonometricFunctions = require('@csstools/postcss-trigonometric-functions');
59
+
60
+ postcss([
61
+ postcssTrigonometricFunctions(/* pluginOptions */)
62
+ ]).process(YOUR_CSS /*, processOptions */);
63
+ ```
64
+
65
+ [PostCSS Trigonometric Functions] runs in all Node environments, with special
66
+ instructions for:
67
+
68
+ | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
69
+ | --- | --- | --- | --- | --- | --- |
70
+
71
+ ## ⚠️ About custom properties
72
+
73
+ Given the dynamic nature of custom properties it's impossible to know what the variable value is, which means the plugin can't compute a final value for the stylesheet.
74
+
75
+ Because of that, any usage that contains a `var` is skipped.
76
+
77
+ ## Units
78
+
79
+ [PostCSS Trigonometric Functions] lets you use different special units that are within the spec and computed at run time to be able to calculate the result of the trigonometric function.
80
+
81
+ The following units are supported:
82
+
83
+ * `pi`: Computes to `Math.PI` which is `3.141592653589793`
84
+ * `e`: Computes to `Math.E` which is `2.718281828459045`
85
+ * `infinity`, `-infinity`: Compute to `Infinity` and `-Infinity` respectively. Note that the usage is case insensitive so `InFiNiTy` is a valid value.
86
+
87
+ Some calculations (such as `sin(-infinity)`) might return `NaN` as per the spec. Given that `NaN` can't be replaced with a value that's useful to CSS it is left as is, as the result will be effectively ignored by the browser.
88
+
89
+ ## Options
90
+
91
+ ### preserve
92
+
93
+ The `preserve` option determines whether the original notation
94
+ is preserved. By default, it is not preserved.
95
+
96
+ ```js
97
+ postcssTrigonometricFunctions({ preserve: true })
98
+ ```
99
+
100
+ ```pcss
101
+ .trigonometry {
102
+ line-height: sin(pi / 4);
103
+ line-height: cos(.125turn);
104
+ line-height: tan(50grad);
105
+ transform: rotate(asin(-1));
106
+ transform: rotate(asin(sin(30deg + 1.0471967rad)));
107
+ transform: rotate(acos(-1));
108
+ transform: rotate(acos(cos(0 / 2 + 1 - 1)));
109
+ transform: rotate(atan(infinity));
110
+ transform: rotate(atan(e - 2.7182818284590452354));
111
+ transform: rotate(atan2(-infinity,-infinity));
112
+ transform: rotate(atan2(-infinity,infinity));
113
+ transform: rotate(atan2(-infinity,infinity));
114
+ transform: rotate(atan2(90, 15));
115
+ }
116
+
117
+ /* becomes */
118
+
119
+ .trigonometry {
120
+ line-height: 0.70711;
121
+ line-height: sin(pi / 4);
122
+ line-height: 0.70711;
123
+ line-height: cos(.125turn);
124
+ line-height: 1;
125
+ line-height: tan(50grad);
126
+ transform: rotate(-90deg);
127
+ transform: rotate(asin(-1));
128
+ transform: rotate(90deg);
129
+ transform: rotate(asin(1));
130
+ transform: rotate(asin(sin(30deg + 1.0471967rad)));
131
+ transform: rotate(180deg);
132
+ transform: rotate(acos(-1));
133
+ transform: rotate(0deg);
134
+ transform: rotate(acos(1));
135
+ transform: rotate(acos(cos(0 / 2 + 1 - 1)));
136
+ transform: rotate(90deg);
137
+ transform: rotate(atan(infinity));
138
+ transform: rotate(0deg);
139
+ transform: rotate(atan(e - 2.7182818284590452354));
140
+ transform: rotate(-135deg);
141
+ transform: rotate(atan2(-infinity,-infinity));
142
+ transform: rotate(-45deg);
143
+ transform: rotate(atan2(-infinity,infinity));
144
+ transform: rotate(-45deg);
145
+ transform: rotate(atan2(-infinity,infinity));
146
+ transform: rotate(80.54deg);
147
+ transform: rotate(atan2(90, 15));
148
+ }
149
+ ```
150
+
151
+ [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
152
+ [css-url]: https://cssdb.org/#trigonometric-functions
153
+ [discord]: https://discord.gg/bUadyRwkJS
154
+ [npm-url]: https://www.npmjs.com/package/@csstools/postcss-trigonometric-functions
155
+
156
+ [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
157
+ [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
158
+ [PostCSS]: https://github.com/postcss/postcss
159
+ [PostCSS Loader]: https://github.com/postcss/postcss-loader
160
+ [PostCSS Trigonometric Functions]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-trigonometric-functions
161
+ [CSS Values 4]: https://www.w3.org/TR/css-values-4/#trig-funcs
package/dist/acos.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Declaration } from 'postcss';
2
+ declare const acosFunctionCheck = "acos(";
3
+ declare function transformAcosFunction(decl: Declaration): string | undefined;
4
+ export { acosFunctionCheck, transformAcosFunction };
package/dist/asin.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Declaration } from 'postcss';
2
+ declare const asinFunctionCheck = "asin(";
3
+ declare function transformAsinFunction(decl: Declaration): string | undefined;
4
+ export { asinFunctionCheck, transformAsinFunction };
package/dist/atan.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Declaration } from 'postcss';
2
+ declare const atanFunctionCheck = "atan(";
3
+ declare function transformAtanFunction(decl: Declaration): string | undefined;
4
+ export { atanFunctionCheck, transformAtanFunction };
@@ -0,0 +1,4 @@
1
+ import type { Declaration } from 'postcss';
2
+ declare const atan2FunctionCheck = "atan2(";
3
+ declare function transformAtan2Function(decl: Declaration): string | undefined;
4
+ export { atan2FunctionCheck, transformAtan2Function };
package/dist/cos.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Declaration } from 'postcss';
2
+ declare const cosFunctionCheck = "cos(";
3
+ declare function transformCosFunction(decl: Declaration): string | undefined;
4
+ export { cosFunctionCheck, transformCosFunction };
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var t=require("postcss-value-parser"),e=require("node:vm");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),u=n(e);function i(t){return t*(180/Math.PI)}const o={turn:function(t){return 2*t*Math.PI},deg:function(t){return t*(Math.PI/180)},grad:function(t){return t*(Math.PI/200)}};function a(t){return"word"===t.type}const s=["+","-","*","/"];var l;function c(t){return!Number.isNaN(t)&&Number.isFinite(t)}function f(t,e=!1){let n=!0;const i=[];if(t.filter((t=>"function"===t.type)).forEach((t=>{var r;if(!n)return;if(""!==t.value)return void(n=!1);const u=f(t.nodes.slice(0),e),i=1===u.length,o=Number((null==(r=u[0])?void 0:r.value)||"");i&&"word"===u[0].type&&!Number.isNaN(o)?(v(t),t.value=u[0].value):n=!1})),!n)return t;const a=t.filter((t=>"word"===t.type||s.includes(t.value)));let d=l.Number;const m=[];let h;const N=(t,e,r)=>{if(d===e){if(e===l.Number){const e=r||"";m.includes(e)||m.push({number:t,unit:e,index:i.length})}i.push(t),d=e===l.Number?l.Operation:l.Number}else n=!1};for(let t=0,u=a.length;t<u&&n;t++){const u=a[t];if(s.includes(u.value)){N(u.value,l.Operation);continue}if("pi"===u.value){N(Math.PI.toString(),l.Number);continue}if("e"===u.value){N(Math.E.toString(),l.Number);continue}const i=r.default.unit(u.value);if(!i){n=!1;break}if(e){if(h||(h=i.unit),h!==i.unit){n=!1;break}N(u.value,l.Operation)}else i.unit?"rad"!==i.unit&&"function"!=typeof o[i.unit]?n=!1:N(i.number,l.Number,i.unit):N(u.value,l.Number)}if(!n)return t;if(i.length%2==0||i.length<3)return t;let b;try{let t="";const e=new Set(m.map((t=>t.unit)));if(e.size>1)if(e.has("")){if(2!==e.size)throw new Error;[t]=Array.from(e).filter((t=>""!==t))}else m.forEach((t=>{if("rad"!==t.unit){const e=o[t.unit](Number(t.number));if(!c(e))throw new Error;i[t.index]=e.toString()}}));const n=u.default.createContext({result:NaN});new u.default.Script(`result = ${i.join(" ")}`).runInContext(n),"number"==typeof n.result&&c(n.result)&&(t&&(n.result=o[t](n.result)),c(n.result)&&(b=n.result))}catch(t){}if(void 0!==b){let e=b.toString();h&&(e+=h);const n=t[0].sourceIndex,r=e.length;t.length=0,t.push({type:"word",value:e,sourceIndex:n,sourceEndIndex:r})}return t}function v(t){delete t.nodes;const e=t;return e.type="word",e}function d(t,e){if(!Number.isNaN(t)){if(t>Number.MAX_SAFE_INTEGER)return"infinity";if(t<Number.MIN_SAFE_INTEGER)return"-infinity"}return Number(t.toFixed(e)).toString()}function m(t){let e,n="";if("infinity"===t.toLowerCase()?e=1/0:"-infinity"===t.toLowerCase()?e=-1/0:"pi"===t?e=Math.PI:"e"===t&&(e=Math.E),!e){const u=r.default.unit(t);if(!u)return!1;e=Number(u.number),Number.isNaN(e)||(n=u.unit)}return{number:e,unit:n}}function h(t,e=!0){t.nodes=f(t.nodes);const n=t.nodes.filter(a);if(1!==t.nodes.length||1!==n.length)return;const{value:r}=n[0],u=m(r);if(!u)return;let i=u.number;if(e){if(u.unit&&"rad"!==u.unit){if(!o[u.unit])return;i=o[u.unit](i)}}else if(u.unit)return;return[v(t),i]}!function(t){t[t.Number=0]="Number",t[t.Operation=1]="Operation"}(l||(l={}));const N=[{check:"asin(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"asin"!==t.value)return;const e=h(t,!1);if(!e)return;const[n,r]=e;let u=Math.asin(r);Number.isNaN(u)||"number"!=typeof u||(u=`${d(i(u),2)}deg`),n.value=u+""})),e.toString()}},{check:"acos(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"acos"!==t.value)return;const e=h(t,!1);if(!e)return;const[n,r]=e;let u=Math.acos(r);Number.isNaN(u)||"number"!=typeof u||(u=`${d(i(u),2)}deg`),n.value=u+""})),e.toString()}},{check:"atan(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"atan"!==t.value)return;const e=h(t,!1);if(!e)return;const[n,r]=e;let u=Math.atan(r);Number.isNaN(u)||"number"!=typeof u||(u=`${d(i(u),2)}deg`),n.value=u+""})),e.toString()}},{check:"atan2(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"atan2"!==t.value)return;const e=t.nodes.findIndex((t=>"div"===t.type&&","===t.value));if(e<0)return;let n=t.nodes.slice(0,e).filter(a),r=t.nodes.slice(e+1).filter(a);if(0===n.length||0===r.length)return;if(n.length>1&&(n=f(n,!0)),r.length>1&&(r=f(r,!0)),1!==n.length||1!==r.length)return;const u=m(n[0].value),o=m(r[0].value);if(!u||!o)return;if(u.unit!==o.unit)return;let s=Math.atan2(u.number,o.number);Number.isNaN(s)||"number"!=typeof s||(s=`${d(i(s),2)}deg`);v(t).value=s+""})),e.toString()}},{check:"sin(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"sin"!==t.value)return;const e=h(t);if(!e)return;const[n,r]=e;n.value=d(Math.sin(r),5)})),e.toString()}},{check:"cos(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"cos"!==t.value)return;const e=h(t);if(!e)return;const[n,r]=e;n.value=d(Math.cos(r),5)})),e.toString()}},{check:"tan(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"tan"!==t.value)return;const e=h(t);if(!e)return;const[n,r]=e,u=Number(d(i(r),2)),o=u/90;n.value=u%90==0&&o%2!=0?o>0?"infinity":"-infinity":d(Math.tan(r),5)})),e.toString()}}],b=t=>{const e=Object.assign({preserve:!1},t);return{postcssPlugin:"postcss-trigonometric-functions",Declaration(t){const n=N.filter((e=>t.value.includes(e.check)));if(!t||0===n.length)return;const r=t.clone();n.forEach((t=>{const e=t.transform(r);e&&(r.value=e)})),t.value!==r.value&&(e.preserve?t.cloneBefore({value:r.value}):t.value=r.value)}}};b.postcss=!0,module.exports=b;
@@ -0,0 +1,6 @@
1
+ import type { PluginCreator } from 'postcss';
2
+ declare type pluginOptions = {
3
+ preserve?: boolean;
4
+ };
5
+ declare const creator: PluginCreator<pluginOptions>;
6
+ export default creator;
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import t from"postcss-value-parser";import n from"node:vm";function e(t){return t*(180/Math.PI)}const r={turn:function(t){return 2*t*Math.PI},deg:function(t){return t*(Math.PI/180)},grad:function(t){return t*(Math.PI/200)}};function u(t){return"word"===t.type}const i=["+","-","*","/"];var o;function a(t){return!Number.isNaN(t)&&Number.isFinite(t)}function s(e,u=!1){let l=!0;const f=[];if(e.filter((t=>"function"===t.type)).forEach((t=>{var n;if(!l)return;if(""!==t.value)return void(l=!1);const e=s(t.nodes.slice(0),u),r=1===e.length,i=Number((null==(n=e[0])?void 0:n.value)||"");r&&"word"===e[0].type&&!Number.isNaN(i)?(c(t),t.value=e[0].value):l=!1})),!l)return e;const v=e.filter((t=>"word"===t.type||i.includes(t.value)));let m=o.Number;const h=[];let N;const d=(t,n,e)=>{if(m===n){if(n===o.Number){const n=e||"";h.includes(n)||h.push({number:t,unit:n,index:f.length})}f.push(t),m=n===o.Number?o.Operation:o.Number}else l=!1};for(let n=0,e=v.length;n<e&&l;n++){const e=v[n];if(i.includes(e.value)){d(e.value,o.Operation);continue}if("pi"===e.value){d(Math.PI.toString(),o.Number);continue}if("e"===e.value){d(Math.E.toString(),o.Number);continue}const a=t.unit(e.value);if(!a){l=!1;break}if(u){if(N||(N=a.unit),N!==a.unit){l=!1;break}d(e.value,o.Operation)}else a.unit?"rad"!==a.unit&&"function"!=typeof r[a.unit]?l=!1:d(a.number,o.Number,a.unit):d(e.value,o.Number)}if(!l)return e;if(f.length%2==0||f.length<3)return e;let b;try{let t="";const e=new Set(h.map((t=>t.unit)));if(e.size>1)if(e.has("")){if(2!==e.size)throw new Error;[t]=Array.from(e).filter((t=>""!==t))}else h.forEach((t=>{if("rad"!==t.unit){const n=r[t.unit](Number(t.number));if(!a(n))throw new Error;f[t.index]=n.toString()}}));const u=n.createContext({result:NaN});new n.Script(`result = ${f.join(" ")}`).runInContext(u),"number"==typeof u.result&&a(u.result)&&(t&&(u.result=r[t](u.result)),a(u.result)&&(b=u.result))}catch(t){}if(void 0!==b){let t=b.toString();N&&(t+=N);const n=e[0].sourceIndex,r=t.length;e.length=0,e.push({type:"word",value:t,sourceIndex:n,sourceEndIndex:r})}return e}function c(t){delete t.nodes;const n=t;return n.type="word",n}function l(t,n){if(!Number.isNaN(t)){if(t>Number.MAX_SAFE_INTEGER)return"infinity";if(t<Number.MIN_SAFE_INTEGER)return"-infinity"}return Number(t.toFixed(n)).toString()}function f(n){let e,r="";if("infinity"===n.toLowerCase()?e=1/0:"-infinity"===n.toLowerCase()?e=-1/0:"pi"===n?e=Math.PI:"e"===n&&(e=Math.E),!e){const u=t.unit(n);if(!u)return!1;e=Number(u.number),Number.isNaN(e)||(r=u.unit)}return{number:e,unit:r}}function v(t,n=!0){t.nodes=s(t.nodes);const e=t.nodes.filter(u);if(1!==t.nodes.length||1!==e.length)return;const{value:i}=e[0],o=f(i);if(!o)return;let a=o.number;if(n){if(o.unit&&"rad"!==o.unit){if(!r[o.unit])return;a=r[o.unit](a)}}else if(o.unit)return;return[c(t),a]}!function(t){t[t.Number=0]="Number",t[t.Operation=1]="Operation"}(o||(o={}));const m=[{check:"asin(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"asin"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.asin(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"acos(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"acos"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.acos(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"atan(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"atan"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.atan(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"atan2(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"atan2"!==t.value)return;const n=t.nodes.findIndex((t=>"div"===t.type&&","===t.value));if(n<0)return;let r=t.nodes.slice(0,n).filter(u),i=t.nodes.slice(n+1).filter(u);if(0===r.length||0===i.length)return;if(r.length>1&&(r=s(r,!0)),i.length>1&&(i=s(i,!0)),1!==r.length||1!==i.length)return;const o=f(r[0].value),a=f(i[0].value);if(!o||!a)return;if(o.unit!==a.unit)return;let v=Math.atan2(o.number,a.number);Number.isNaN(v)||"number"!=typeof v||(v=`${l(e(v),2)}deg`);c(t).value=v+""})),r.toString()}},{check:"sin(",transform:function(n){const e=t(n.value);return e.walk((t=>{if("function"!==t.type||"sin"!==t.value)return;const n=v(t);if(!n)return;const[e,r]=n;e.value=l(Math.sin(r),5)})),e.toString()}},{check:"cos(",transform:function(n){const e=t(n.value);return e.walk((t=>{if("function"!==t.type||"cos"!==t.value)return;const n=v(t);if(!n)return;const[e,r]=n;e.value=l(Math.cos(r),5)})),e.toString()}},{check:"tan(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"tan"!==t.value)return;const n=v(t);if(!n)return;const[r,u]=n,i=Number(l(e(u),2)),o=i/90;r.value=i%90==0&&o%2!=0?o>0?"infinity":"-infinity":l(Math.tan(u),5)})),r.toString()}}],h=t=>{const n=Object.assign({preserve:!1},t);return{postcssPlugin:"postcss-trigonometric-functions",Declaration(t){const e=m.filter((n=>t.value.includes(n.check)));if(!t||0===e.length)return;const r=t.clone();e.forEach((t=>{const n=t.transform(r);n&&(r.value=n)})),t.value!==r.value&&(n.preserve?t.cloneBefore({value:r.value}):t.value=r.value)}}};h.postcss=!0;export{h as default};
package/dist/sin.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Declaration } from 'postcss';
2
+ declare const sinFunctionCheck = "sin(";
3
+ declare function transformSinFunction(decl: Declaration): string | undefined;
4
+ export { sinFunctionCheck, transformSinFunction };
package/dist/tan.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Declaration } from 'postcss';
2
+ declare const tanFunctionCheck = "tan(";
3
+ declare function transformTanFunction(decl: Declaration): string | undefined;
4
+ export { tanFunctionCheck, transformTanFunction };
@@ -0,0 +1,61 @@
1
+ import type { FunctionNode, WordNode, Node } from 'postcss-value-parser';
2
+ export declare function turnToRad(turn: number): number;
3
+ export declare function degToRad(deg: number): number;
4
+ export declare function gradToRad(grad: number): number;
5
+ export declare function radToDeg(rad: number): number;
6
+ export declare function gradToDeg(grad: number): number;
7
+ export declare function turnToDeg(turn: number): number;
8
+ declare const toRad: {
9
+ turn: typeof turnToRad;
10
+ deg: typeof degToRad;
11
+ grad: typeof gradToRad;
12
+ };
13
+ declare const toDeg: {
14
+ grad: typeof gradToDeg;
15
+ turn: typeof turnToDeg;
16
+ rad: typeof radToDeg;
17
+ };
18
+ export declare function filterOnlyWords(node: Node): boolean;
19
+ /**
20
+ * Try to compute a calculation from a Node.
21
+ *
22
+ * This validates that the calculation has a valid order which is:
23
+ * - `{Number} {Operation} {Number} ...`
24
+ *
25
+ * Only basic arithmetic operations are allowed, and it has to be separate words
26
+ * similarly to how CSS calc works:
27
+ *
28
+ * - `sin(3.14159 * 2)` -> is valid
29
+ * - `sin(3.14159*2)` -> is not valid
30
+ *
31
+ *
32
+ * @param {FunctionNode} nodes Nodes to be parsed
33
+ * @param {Boolean} ignoreUnit Whether units are ignored or converted to radians
34
+ * @return {FunctionNode} Returns the node, if it managed to calculate, it will
35
+ * simplify inner nodes.
36
+ * @see https://www.w3.org/TR/css-values-4/#trig-funcs
37
+ */
38
+ export declare function computeCalculation(nodes: Node[], ignoreUnit?: boolean): Node[];
39
+ export declare function functionNodeToWordNode(fn: FunctionNode): WordNode;
40
+ /**
41
+ * Formats a number that's intended to be put into CSS.
42
+ *
43
+ * Due to processing of Number(number.toFixed(decimals)) this will get
44
+ * rid of ending zeroes, usually helping with the rounding which is the
45
+ * intended effect.
46
+ *
47
+ * For example, converting 4.71238898038469 radians into deg leads to
48
+ * 270.000000000669786 which is going to result as 270 unless a
49
+ * precision of 10 is chosen.
50
+ *
51
+ * @param {Number} number Number to be formatted
52
+ * @param {Number} decimals Precision of decimals, CSS doesn't usually handle further than 5.
53
+ */
54
+ export declare function formatResultingNumber(number: number, decimals: number): string;
55
+ export declare function parseNumber(value: string): false | {
56
+ number: any;
57
+ unit: string;
58
+ };
59
+ declare type validateNodeReturn = [WordNode, number] | undefined;
60
+ export declare function validateNode(node: FunctionNode, parseUnit?: boolean): validateNodeReturn;
61
+ export { toRad, toDeg };
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@csstools/postcss-trigonometric-functions",
3
+ "description": "Use sin(), cos(), tan(), acos(), atan(), and atan2() to compute trigonometric relationships",
4
+ "version": "1.0.0",
5
+ "contributors": [
6
+ {
7
+ "name": "Antonio Laguna",
8
+ "email": "antonio@laguna.es",
9
+ "url": "https://antonio.laguna.es"
10
+ },
11
+ {
12
+ "name": "Romain Menke ",
13
+ "email": "romainmenke@gmail.com"
14
+ }
15
+ ],
16
+ "license": "CC0-1.0",
17
+ "funding": {
18
+ "type": "opencollective",
19
+ "url": "https://opencollective.com/csstools"
20
+ },
21
+ "engines": {
22
+ "node": "^14 || >=16"
23
+ },
24
+ "main": "dist/index.cjs",
25
+ "module": "dist/index.mjs",
26
+ "types": "dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "import": "./dist/index.mjs",
30
+ "require": "./dist/index.cjs",
31
+ "default": "./dist/index.mjs"
32
+ }
33
+ },
34
+ "files": [
35
+ "CHANGELOG.md",
36
+ "LICENSE.md",
37
+ "README.md",
38
+ "dist"
39
+ ],
40
+ "dependencies": {
41
+ "postcss-value-parser": "^4.2.0"
42
+ },
43
+ "peerDependencies": {
44
+ "postcss": "^8.4"
45
+ },
46
+ "scripts": {
47
+ "build": "rollup -c ../../rollup/default.js",
48
+ "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true });\"",
49
+ "docs": "node ../../.github/bin/generate-docs/install.mjs && node ../../.github/bin/generate-docs/readme.mjs",
50
+ "lint": "npm run lint:eslint && npm run lint:package-json",
51
+ "lint:eslint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
52
+ "lint:package-json": "node ../../.github/bin/format-package-json.mjs",
53
+ "prepublishOnly": "npm run clean && npm run build && npm run test",
54
+ "test": "node .tape.mjs && npm run test:exports",
55
+ "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs",
56
+ "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs"
57
+ },
58
+ "homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-trigonometric-functions#readme",
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "https://github.com/csstools/postcss-plugins.git",
62
+ "directory": "plugins/postcss-trigonometric-functions"
63
+ },
64
+ "bugs": "https://github.com/csstools/postcss-plugins/issues",
65
+ "keywords": [
66
+ "acos",
67
+ "acos2",
68
+ "asin",
69
+ "atan",
70
+ "cos",
71
+ "css",
72
+ "postcss-plugin",
73
+ "sin",
74
+ "tan",
75
+ "trigonometric"
76
+ ],
77
+ "csstools": {
78
+ "cssdbId": "trigonometric-functions",
79
+ "exportName": "postcssTrigonometricFunctions",
80
+ "humanReadableName": "PostCSS Trigonometric Functions",
81
+ "specUrl": "https://www.w3.org/TR/css-values-4/#trig-funcs"
82
+ },
83
+ "volta": {
84
+ "extends": "../../package.json"
85
+ }
86
+ }