@tremolo-ui/functions 0.1.2 → 0.1.3

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.
Files changed (122) hide show
  1. package/dist/NumberInput/type.cjs +45 -0
  2. package/dist/NumberInput/type.cjs.map +1 -0
  3. package/dist/NumberInput/type.d.cts +20 -0
  4. package/dist/NumberInput/type.d.cts.map +1 -0
  5. package/dist/NumberInput/type.d.ts +20 -0
  6. package/dist/NumberInput/type.d.ts.map +1 -0
  7. package/dist/NumberInput/type.js +43 -0
  8. package/dist/NumberInput/type.js.map +1 -0
  9. package/dist/Slider/type.cjs +36 -0
  10. package/dist/Slider/type.cjs.map +1 -0
  11. package/dist/Slider/type.d.cts +15 -0
  12. package/dist/Slider/type.d.cts.map +1 -0
  13. package/dist/Slider/type.d.ts +15 -0
  14. package/dist/Slider/type.d.ts.map +1 -0
  15. package/dist/Slider/type.js +35 -0
  16. package/dist/Slider/type.js.map +1 -0
  17. package/dist/index.cjs +264 -0
  18. package/dist/index.cjs.map +1 -0
  19. package/dist/index.d.cts +161 -0
  20. package/dist/index.d.cts.map +1 -0
  21. package/dist/index.d.ts +161 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +238 -0
  24. package/dist/index.js.map +1 -0
  25. package/package.json +17 -17
  26. package/src/index.ts +30 -4
  27. package/dist/cjs/components/NumberInput/type.js +0 -51
  28. package/dist/cjs/components/NumberInput/type.js.map +0 -1
  29. package/dist/cjs/components/NumberInput/types/components/NumberInput/type.d.ts +0 -16
  30. package/dist/cjs/components/NumberInput/types/components/NumberInput/type.d.ts.map +0 -1
  31. package/dist/cjs/components/NumberInput/types/components/Slider/type.d.ts +0 -10
  32. package/dist/cjs/components/NumberInput/types/components/Slider/type.d.ts.map +0 -1
  33. package/dist/cjs/components/NumberInput/types/index.d.ts +0 -13
  34. package/dist/cjs/components/NumberInput/types/index.d.ts.map +0 -1
  35. package/dist/cjs/components/NumberInput/types/math.d.ts +0 -66
  36. package/dist/cjs/components/NumberInput/types/math.d.ts.map +0 -1
  37. package/dist/cjs/components/NumberInput/types/midi.d.ts +0 -61
  38. package/dist/cjs/components/NumberInput/types/midi.d.ts.map +0 -1
  39. package/dist/cjs/components/NumberInput/types/types.d.ts +0 -6
  40. package/dist/cjs/components/NumberInput/types/types.d.ts.map +0 -1
  41. package/dist/cjs/components/NumberInput/types/util.d.ts +0 -20
  42. package/dist/cjs/components/NumberInput/types/util.d.ts.map +0 -1
  43. package/dist/cjs/components/Slider/type.js +0 -38
  44. package/dist/cjs/components/Slider/type.js.map +0 -1
  45. package/dist/cjs/components/Slider/types/components/NumberInput/type.d.ts +0 -16
  46. package/dist/cjs/components/Slider/types/components/NumberInput/type.d.ts.map +0 -1
  47. package/dist/cjs/components/Slider/types/components/Slider/type.d.ts +0 -10
  48. package/dist/cjs/components/Slider/types/components/Slider/type.d.ts.map +0 -1
  49. package/dist/cjs/components/Slider/types/index.d.ts +0 -13
  50. package/dist/cjs/components/Slider/types/index.d.ts.map +0 -1
  51. package/dist/cjs/components/Slider/types/math.d.ts +0 -66
  52. package/dist/cjs/components/Slider/types/math.d.ts.map +0 -1
  53. package/dist/cjs/components/Slider/types/midi.d.ts +0 -61
  54. package/dist/cjs/components/Slider/types/midi.d.ts.map +0 -1
  55. package/dist/cjs/components/Slider/types/types.d.ts +0 -6
  56. package/dist/cjs/components/Slider/types/types.d.ts.map +0 -1
  57. package/dist/cjs/components/Slider/types/util.d.ts +0 -20
  58. package/dist/cjs/components/Slider/types/util.d.ts.map +0 -1
  59. package/dist/cjs/index.js +0 -282
  60. package/dist/cjs/index.js.map +0 -1
  61. package/dist/cjs/types/components/NumberInput/type.d.ts +0 -16
  62. package/dist/cjs/types/components/NumberInput/type.d.ts.map +0 -1
  63. package/dist/cjs/types/components/Slider/type.d.ts +0 -10
  64. package/dist/cjs/types/components/Slider/type.d.ts.map +0 -1
  65. package/dist/cjs/types/index.d.ts +0 -13
  66. package/dist/cjs/types/index.d.ts.map +0 -1
  67. package/dist/cjs/types/math.d.ts +0 -66
  68. package/dist/cjs/types/math.d.ts.map +0 -1
  69. package/dist/cjs/types/midi.d.ts +0 -61
  70. package/dist/cjs/types/midi.d.ts.map +0 -1
  71. package/dist/cjs/types/types.d.ts +0 -6
  72. package/dist/cjs/types/types.d.ts.map +0 -1
  73. package/dist/cjs/types/util.d.ts +0 -20
  74. package/dist/cjs/types/util.d.ts.map +0 -1
  75. package/dist/esm/components/NumberInput/type.js +0 -48
  76. package/dist/esm/components/NumberInput/type.js.map +0 -1
  77. package/dist/esm/components/NumberInput/types/components/NumberInput/type.d.ts +0 -16
  78. package/dist/esm/components/NumberInput/types/components/NumberInput/type.d.ts.map +0 -1
  79. package/dist/esm/components/NumberInput/types/components/Slider/type.d.ts +0 -10
  80. package/dist/esm/components/NumberInput/types/components/Slider/type.d.ts.map +0 -1
  81. package/dist/esm/components/NumberInput/types/index.d.ts +0 -13
  82. package/dist/esm/components/NumberInput/types/index.d.ts.map +0 -1
  83. package/dist/esm/components/NumberInput/types/math.d.ts +0 -66
  84. package/dist/esm/components/NumberInput/types/math.d.ts.map +0 -1
  85. package/dist/esm/components/NumberInput/types/midi.d.ts +0 -61
  86. package/dist/esm/components/NumberInput/types/midi.d.ts.map +0 -1
  87. package/dist/esm/components/NumberInput/types/types.d.ts +0 -6
  88. package/dist/esm/components/NumberInput/types/types.d.ts.map +0 -1
  89. package/dist/esm/components/NumberInput/types/util.d.ts +0 -20
  90. package/dist/esm/components/NumberInput/types/util.d.ts.map +0 -1
  91. package/dist/esm/components/Slider/type.js +0 -36
  92. package/dist/esm/components/Slider/type.js.map +0 -1
  93. package/dist/esm/components/Slider/types/components/NumberInput/type.d.ts +0 -16
  94. package/dist/esm/components/Slider/types/components/NumberInput/type.d.ts.map +0 -1
  95. package/dist/esm/components/Slider/types/components/Slider/type.d.ts +0 -10
  96. package/dist/esm/components/Slider/types/components/Slider/type.d.ts.map +0 -1
  97. package/dist/esm/components/Slider/types/index.d.ts +0 -13
  98. package/dist/esm/components/Slider/types/index.d.ts.map +0 -1
  99. package/dist/esm/components/Slider/types/math.d.ts +0 -66
  100. package/dist/esm/components/Slider/types/math.d.ts.map +0 -1
  101. package/dist/esm/components/Slider/types/midi.d.ts +0 -61
  102. package/dist/esm/components/Slider/types/midi.d.ts.map +0 -1
  103. package/dist/esm/components/Slider/types/types.d.ts +0 -6
  104. package/dist/esm/components/Slider/types/types.d.ts.map +0 -1
  105. package/dist/esm/components/Slider/types/util.d.ts +0 -20
  106. package/dist/esm/components/Slider/types/util.d.ts.map +0 -1
  107. package/dist/esm/index.js +0 -255
  108. package/dist/esm/index.js.map +0 -1
  109. package/dist/esm/types/components/NumberInput/type.d.ts +0 -16
  110. package/dist/esm/types/components/NumberInput/type.d.ts.map +0 -1
  111. package/dist/esm/types/components/Slider/type.d.ts +0 -10
  112. package/dist/esm/types/components/Slider/type.d.ts.map +0 -1
  113. package/dist/esm/types/index.d.ts +0 -13
  114. package/dist/esm/types/index.d.ts.map +0 -1
  115. package/dist/esm/types/math.d.ts +0 -66
  116. package/dist/esm/types/math.d.ts.map +0 -1
  117. package/dist/esm/types/midi.d.ts +0 -61
  118. package/dist/esm/types/midi.d.ts.map +0 -1
  119. package/dist/esm/types/types.d.ts +0 -6
  120. package/dist/esm/types/types.d.ts.map +0 -1
  121. package/dist/esm/types/util.d.ts +0 -20
  122. package/dist/esm/types/util.d.ts.map +0 -1
@@ -0,0 +1,45 @@
1
+
2
+ //#region src/components/NumberInput/type.ts
3
+ function selectUnit(units, value) {
4
+ let i = 0;
5
+ for (; i < units.length; i++) if (Math.abs(units[i][1]) > Math.abs(value)) break;
6
+ return units[Math.max(0, i - 1)];
7
+ }
8
+ function parseValue(inputString, units, digit) {
9
+ const str = inputString.trim();
10
+ if (!units || typeof units == "string") {
11
+ const m$1 = str.match(/-?\d+(\.\d+)?/);
12
+ let v = Number(m$1?.[0] ?? "0");
13
+ v = isNaN(v) ? 0 : v;
14
+ return {
15
+ rawValue: v,
16
+ formatValue: (digit != void 0 ? v.toFixed(digit) : v) + (units ?? ""),
17
+ unit: units ?? ""
18
+ };
19
+ }
20
+ const unitList = units.map(([u, _s]) => u);
21
+ const scaleList = units.map(([_u, s]) => s);
22
+ let rawValue = 0;
23
+ let unit = unitList[0];
24
+ let formatValue = (digit != void 0 ? rawValue.toFixed(digit) : rawValue) + unit;
25
+ const m = str.match(/^(-?\d+(\.\d+)?)\s*(\w*)$/);
26
+ if (m) {
27
+ rawValue = Number(m[1]) || 0;
28
+ const uIndex = unitList.indexOf(m[3]);
29
+ rawValue *= uIndex != -1 ? scaleList[uIndex] : 1;
30
+ const [u, scale] = selectUnit(units, rawValue);
31
+ const v = rawValue / scale;
32
+ formatValue = (digit != void 0 ? v.toFixed(digit) : v) + u;
33
+ unit = u;
34
+ }
35
+ return {
36
+ rawValue,
37
+ formatValue,
38
+ unit
39
+ };
40
+ }
41
+
42
+ //#endregion
43
+ exports.parseValue = parseValue;
44
+ exports.selectUnit = selectUnit;
45
+ //# sourceMappingURL=type.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.cjs","names":["units: Units","value: number","inputString: string","units?: string | Units","digit?: number","m"],"sources":["../../src/components/NumberInput/type.ts"],"sourcesContent":["/**\n * @module NumberInput\n */\n\n/**\n * [unit, scale][]\n * @example\n * [['Hz', 1], ['kHz', 1000]]\n */\nexport type Units = [string, number][]\n\nexport function selectUnit(units: Units, value: number): [string, number] {\n let i = 0\n for (; i < units.length; i++) {\n if (Math.abs(units[i][1]) > Math.abs(value)) break\n }\n return units[Math.max(0, i - 1)]\n}\n\nexport function parseValue(\n inputString: string,\n units?: string | Units,\n digit?: number,\n): {\n rawValue: number\n formatValue: string\n unit: string\n} {\n const str = inputString.trim()\n if (!units || typeof units == 'string') {\n const m = str.match(/-?\\d+(\\.\\d+)?/)\n let v = Number(m?.[0] ?? '0')\n v = isNaN(v) ? 0 : v\n return {\n rawValue: v,\n formatValue: (digit != undefined ? v.toFixed(digit) : v) + (units ?? ''),\n unit: units ?? '',\n }\n }\n\n const unitList = units.map(([u, _s]) => u)\n const scaleList = units.map(([_u, s]) => s)\n let rawValue = 0\n let unit = unitList[0]\n let formatValue =\n (digit != undefined ? rawValue.toFixed(digit) : rawValue) + unit\n const m = str.match(/^(-?\\d+(\\.\\d+)?)\\s*(\\w*)$/)\n if (m) {\n rawValue = Number(m[1]) || 0\n const uIndex = unitList.indexOf(m[3])\n rawValue *= uIndex != -1 ? scaleList[uIndex] : 1\n const [u, scale] = selectUnit(units, rawValue)\n const v = rawValue / scale\n formatValue = (digit != undefined ? v.toFixed(digit) : v) + u\n unit = u\n }\n return {\n rawValue: rawValue,\n formatValue: formatValue,\n unit: unit,\n }\n}\n"],"mappings":";;AAWA,SAAgB,WAAWA,OAAcC,OAAiC;CACxE,IAAI,IAAI;AACR,QAAO,IAAI,MAAM,QAAQ,IACvB,KAAI,KAAK,IAAI,MAAM,GAAG,GAAG,GAAG,KAAK,IAAI,MAAM,CAAE;AAE/C,QAAO,MAAM,KAAK,IAAI,GAAG,IAAI,EAAE;AAChC;AAED,SAAgB,WACdC,aACAC,OACAC,OAKA;CACA,MAAM,MAAM,YAAY,MAAM;AAC9B,MAAK,gBAAgB,SAAS,UAAU;EACtC,MAAMC,MAAI,IAAI,MAAM,gBAAgB;EACpC,IAAI,IAAI,OAAOA,MAAI,MAAM,IAAI;AAC7B,MAAI,MAAM,EAAE,GAAG,IAAI;AACnB,SAAO;GACL,UAAU;GACV,cAAc,kBAAqB,EAAE,QAAQ,MAAM,GAAG,MAAM,SAAS;GACrE,MAAM,SAAS;EAChB;CACF;CAED,MAAM,WAAW,MAAM,IAAI,CAAC,CAAC,GAAG,GAAG,KAAK,EAAE;CAC1C,MAAM,YAAY,MAAM,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE;CAC3C,IAAI,WAAW;CACf,IAAI,OAAO,SAAS;CACpB,IAAI,eACD,kBAAqB,SAAS,QAAQ,MAAM,GAAG,YAAY;CAC9D,MAAM,IAAI,IAAI,MAAM,4BAA4B;AAChD,KAAI,GAAG;AACL,aAAW,OAAO,EAAE,GAAG,IAAI;EAC3B,MAAM,SAAS,SAAS,QAAQ,EAAE,GAAG;AACrC,cAAY,UAAU,KAAK,UAAU,UAAU;EAC/C,MAAM,CAAC,GAAG,MAAM,GAAG,WAAW,OAAO,SAAS;EAC9C,MAAM,IAAI,WAAW;AACrB,iBAAe,kBAAqB,EAAE,QAAQ,MAAM,GAAG,KAAK;AAC5D,SAAO;CACR;AACD,QAAO;EACK;EACG;EACP;CACP;AACF"}
@@ -0,0 +1,20 @@
1
+ //#region src/components/NumberInput/type.d.ts
2
+ /**
3
+ * @module NumberInput
4
+ */
5
+ /**
6
+ * [unit, scale][]
7
+ * @example
8
+ * [['Hz', 1], ['kHz', 1000]]
9
+ */
10
+ type Units = [string, number][];
11
+ declare function selectUnit(units: Units, value: number): [string, number];
12
+ declare function parseValue(inputString: string, units?: string | Units, digit?: number): {
13
+ rawValue: number;
14
+ formatValue: string;
15
+ unit: string;
16
+ };
17
+ //# sourceMappingURL=type.d.ts.map
18
+ //#endregion
19
+ export { Units, parseValue, selectUnit };
20
+ //# sourceMappingURL=type.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.cts","names":[],"sources":["../../../src/components/NumberInput/type.ts"],"sourcesContent":[],"mappings":";;AASA;AAEA;AAQA;;;;;KAVY,KAAA;iBAEI,UAAA,QAAkB;iBAQlB,UAAA,uCAEG"}
@@ -0,0 +1,20 @@
1
+ //#region src/components/NumberInput/type.d.ts
2
+ /**
3
+ * @module NumberInput
4
+ */
5
+ /**
6
+ * [unit, scale][]
7
+ * @example
8
+ * [['Hz', 1], ['kHz', 1000]]
9
+ */
10
+ type Units = [string, number][];
11
+ declare function selectUnit(units: Units, value: number): [string, number];
12
+ declare function parseValue(inputString: string, units?: string | Units, digit?: number): {
13
+ rawValue: number;
14
+ formatValue: string;
15
+ unit: string;
16
+ };
17
+ //# sourceMappingURL=type.d.ts.map
18
+ //#endregion
19
+ export { Units, parseValue, selectUnit };
20
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","names":[],"sources":["../../../src/components/NumberInput/type.ts"],"sourcesContent":[],"mappings":";;AASA;AAEA;AAQA;;;;;KAVY,KAAA;iBAEI,UAAA,QAAkB;iBAQlB,UAAA,uCAEG"}
@@ -0,0 +1,43 @@
1
+ //#region src/components/NumberInput/type.ts
2
+ function selectUnit(units, value) {
3
+ let i = 0;
4
+ for (; i < units.length; i++) if (Math.abs(units[i][1]) > Math.abs(value)) break;
5
+ return units[Math.max(0, i - 1)];
6
+ }
7
+ function parseValue(inputString, units, digit) {
8
+ const str = inputString.trim();
9
+ if (!units || typeof units == "string") {
10
+ const m$1 = str.match(/-?\d+(\.\d+)?/);
11
+ let v = Number(m$1?.[0] ?? "0");
12
+ v = isNaN(v) ? 0 : v;
13
+ return {
14
+ rawValue: v,
15
+ formatValue: (digit != void 0 ? v.toFixed(digit) : v) + (units ?? ""),
16
+ unit: units ?? ""
17
+ };
18
+ }
19
+ const unitList = units.map(([u, _s]) => u);
20
+ const scaleList = units.map(([_u, s]) => s);
21
+ let rawValue = 0;
22
+ let unit = unitList[0];
23
+ let formatValue = (digit != void 0 ? rawValue.toFixed(digit) : rawValue) + unit;
24
+ const m = str.match(/^(-?\d+(\.\d+)?)\s*(\w*)$/);
25
+ if (m) {
26
+ rawValue = Number(m[1]) || 0;
27
+ const uIndex = unitList.indexOf(m[3]);
28
+ rawValue *= uIndex != -1 ? scaleList[uIndex] : 1;
29
+ const [u, scale] = selectUnit(units, rawValue);
30
+ const v = rawValue / scale;
31
+ formatValue = (digit != void 0 ? v.toFixed(digit) : v) + u;
32
+ unit = u;
33
+ }
34
+ return {
35
+ rawValue,
36
+ formatValue,
37
+ unit
38
+ };
39
+ }
40
+
41
+ //#endregion
42
+ export { parseValue, selectUnit };
43
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","names":["units: Units","value: number","inputString: string","units?: string | Units","digit?: number","m"],"sources":["../../src/components/NumberInput/type.ts"],"sourcesContent":["/**\n * @module NumberInput\n */\n\n/**\n * [unit, scale][]\n * @example\n * [['Hz', 1], ['kHz', 1000]]\n */\nexport type Units = [string, number][]\n\nexport function selectUnit(units: Units, value: number): [string, number] {\n let i = 0\n for (; i < units.length; i++) {\n if (Math.abs(units[i][1]) > Math.abs(value)) break\n }\n return units[Math.max(0, i - 1)]\n}\n\nexport function parseValue(\n inputString: string,\n units?: string | Units,\n digit?: number,\n): {\n rawValue: number\n formatValue: string\n unit: string\n} {\n const str = inputString.trim()\n if (!units || typeof units == 'string') {\n const m = str.match(/-?\\d+(\\.\\d+)?/)\n let v = Number(m?.[0] ?? '0')\n v = isNaN(v) ? 0 : v\n return {\n rawValue: v,\n formatValue: (digit != undefined ? v.toFixed(digit) : v) + (units ?? ''),\n unit: units ?? '',\n }\n }\n\n const unitList = units.map(([u, _s]) => u)\n const scaleList = units.map(([_u, s]) => s)\n let rawValue = 0\n let unit = unitList[0]\n let formatValue =\n (digit != undefined ? rawValue.toFixed(digit) : rawValue) + unit\n const m = str.match(/^(-?\\d+(\\.\\d+)?)\\s*(\\w*)$/)\n if (m) {\n rawValue = Number(m[1]) || 0\n const uIndex = unitList.indexOf(m[3])\n rawValue *= uIndex != -1 ? scaleList[uIndex] : 1\n const [u, scale] = selectUnit(units, rawValue)\n const v = rawValue / scale\n formatValue = (digit != undefined ? v.toFixed(digit) : v) + u\n unit = u\n }\n return {\n rawValue: rawValue,\n formatValue: formatValue,\n unit: unit,\n }\n}\n"],"mappings":";AAWA,SAAgB,WAAWA,OAAcC,OAAiC;CACxE,IAAI,IAAI;AACR,QAAO,IAAI,MAAM,QAAQ,IACvB,KAAI,KAAK,IAAI,MAAM,GAAG,GAAG,GAAG,KAAK,IAAI,MAAM,CAAE;AAE/C,QAAO,MAAM,KAAK,IAAI,GAAG,IAAI,EAAE;AAChC;AAED,SAAgB,WACdC,aACAC,OACAC,OAKA;CACA,MAAM,MAAM,YAAY,MAAM;AAC9B,MAAK,gBAAgB,SAAS,UAAU;EACtC,MAAMC,MAAI,IAAI,MAAM,gBAAgB;EACpC,IAAI,IAAI,OAAOA,MAAI,MAAM,IAAI;AAC7B,MAAI,MAAM,EAAE,GAAG,IAAI;AACnB,SAAO;GACL,UAAU;GACV,cAAc,kBAAqB,EAAE,QAAQ,MAAM,GAAG,MAAM,SAAS;GACrE,MAAM,SAAS;EAChB;CACF;CAED,MAAM,WAAW,MAAM,IAAI,CAAC,CAAC,GAAG,GAAG,KAAK,EAAE;CAC1C,MAAM,YAAY,MAAM,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE;CAC3C,IAAI,WAAW;CACf,IAAI,OAAO,SAAS;CACpB,IAAI,eACD,kBAAqB,SAAS,QAAQ,MAAM,GAAG,YAAY;CAC9D,MAAM,IAAI,IAAI,MAAM,4BAA4B;AAChD,KAAI,GAAG;AACL,aAAW,OAAO,EAAE,GAAG,IAAI;EAC3B,MAAM,SAAS,SAAS,QAAQ,EAAE,GAAG;AACrC,cAAY,UAAU,KAAK,UAAU,UAAU;EAC/C,MAAM,CAAC,GAAG,MAAM,GAAG,WAAW,OAAO,SAAS;EAC9C,MAAM,IAAI,WAAW;AACrB,iBAAe,kBAAqB,EAAE,QAAQ,MAAM,GAAG,KAAK;AAC5D,SAAO;CACR;AACD,QAAO;EACK;EACG;EACP;CACP;AACF"}
@@ -0,0 +1,36 @@
1
+
2
+ //#region src/math.ts
3
+ /**
4
+ *
5
+ * @category Math
6
+ */
7
+ function toFixed(x, fractionDigits) {
8
+ return Number(x.toFixed(fractionDigits));
9
+ }
10
+ /**
11
+ *
12
+ * @category Math
13
+ */
14
+ function decimalPart(x) {
15
+ return String(x).split(".")[1];
16
+ }
17
+
18
+ //#endregion
19
+ //#region src/components/Slider/type.ts
20
+ function generateOptionsList(options, min, max, step) {
21
+ const optionsList = [];
22
+ const per = options[0] == "step" ? step : options[0];
23
+ const count = Math.floor(max / per) - Math.ceil(min / per) + 1;
24
+ for (let i = 0; i < count; i++) {
25
+ const value = toFixed(per * (Math.ceil(min / per) + i), decimalPart(per)?.length);
26
+ optionsList.push({
27
+ value,
28
+ type: options[1]
29
+ });
30
+ }
31
+ return optionsList;
32
+ }
33
+
34
+ //#endregion
35
+ exports.generateOptionsList = generateOptionsList;
36
+ //# sourceMappingURL=type.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.cjs","names":["x: number","fractionDigits?: number","x: number | string","options: ScaleOptions","min: number","max: number","step: number","optionsList: {\n value: number\n type: ScaleType\n }[]"],"sources":["../../src/math.ts","../../src/components/Slider/type.ts"],"sourcesContent":["/**\n * clamp value between min and max\n * @category Math\n */\nexport function clamp(value: number, min: number, max: number) {\n return Math.max(min, Math.min(value, max))\n}\n\n/**\n * Normalize the value from 0 to 1\n * @category Math\n */\nexport function normalizeValue(\n rawValue: number,\n min: number,\n max: number,\n skew = 1,\n) {\n if (min >= max) throw new RangeError('requirements: min < max')\n const v = clamp((rawValue - min) / (max - min), 0, 1)\n return Math.pow(v, skew)\n}\n\n/**\n * Convert normalized values back to raw values.\n * @category Math\n */\nexport function rawValue(\n normalizedValue: number,\n min: number,\n max: number,\n skew = 1,\n) {\n if (min >= max) throw new RangeError('requirements: min < max')\n const v =\n skew == 1\n ? clamp(normalizedValue, 0, 1)\n : Math.exp(Math.log(clamp(normalizedValue, 0, 1)) / skew)\n return min + v * (max - min)\n}\n\n/**\n *\n * @category Math\n */\nexport function skewWithCenterValue(\n centerValue: number,\n min: number,\n max: number,\n) {\n if (!(min <= centerValue && centerValue <= max))\n throw new RangeError('requirements: min <= centerValue <= max')\n return Math.log(0.5) / Math.log((centerValue - min) / (max - min))\n}\n\n/**\n *\n * @category Math\n */\nexport function stepValue(value: number, step: number) {\n if (step <= 0) throw new RangeError('requirements: step > 0')\n const quotient = Math.floor(value / step)\n const decimalDigits = decimalPart(step)?.length\n const v = toFixed(quotient * step, decimalDigits)\n const next = toFixed((quotient + 1) * step, decimalDigits)\n return Math.abs(value - v) < Math.abs(value - next) ? v : next\n}\n\n/**\n *\n * @category Math\n */\nexport function toFixed(x: number, fractionDigits?: number) {\n return Number(x.toFixed(fractionDigits))\n}\n\n/**\n *\n * @category Math\n */\nexport function integerPart(x: number | string): string | undefined {\n if (Number.isNaN(x)) {\n return undefined\n }\n return String(x).split('.')[0]\n}\n\n/**\n *\n * @category Math\n */\nexport function decimalPart(x: number | string): string | undefined {\n return String(x).split('.')[1]\n}\n\n/**\n *\n * @category Math\n */\nexport function radian(degree: number) {\n return (Math.PI * degree) / 180\n}\n\n/**\n *\n * @category Math\n */\nexport function degree(radian: number) {\n return (180 * radian) / Math.PI\n}\n\n/**\n *\n * @category Math\n */\nexport function mapValue(\n value: number,\n inMin: number,\n inMax: number,\n outMin: number,\n outMax: number,\n) {\n return ((value - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin\n}\n\n/**\n *\n * @category Math\n */\nexport function dbToGain(db: number) {\n return Math.pow(10, db / 20)\n}\n\n/**\n *\n * @category Math\n */\nexport function gainToDb(gain: number) {\n return 20 * (Math.log(gain) / Math.LN10)\n}\n","/**\n * @module Slider\n */\n\nimport { decimalPart, toFixed } from '../../math'\n\nexport type ScaleType = 'mark' | 'mark-number' | 'number'\nexport type ScaleOptions = ['step', ScaleType] | [number, ScaleType]\n\nexport function generateOptionsList(\n options: ScaleOptions,\n min: number,\n max: number,\n step: number,\n) {\n const optionsList: {\n value: number\n type: ScaleType\n }[] = []\n const per = options[0] == 'step' ? step : options[0]\n const count = Math.floor(max / per) - Math.ceil(min / per) + 1\n for (let i = 0; i < count; i++) {\n const value = toFixed(\n per * (Math.ceil(min / per) + i),\n decimalPart(per)?.length,\n )\n optionsList.push({ value: value, type: options[1] })\n }\n return optionsList\n}\n"],"mappings":";;;;;;AAwEA,SAAgB,QAAQA,GAAWC,gBAAyB;AAC1D,QAAO,OAAO,EAAE,QAAQ,eAAe,CAAC;AACzC;;;;;AAiBD,SAAgB,YAAYC,GAAwC;AAClE,QAAO,OAAO,EAAE,CAAC,MAAM,IAAI,CAAC;AAC7B;;;;ACpFD,SAAgB,oBACdC,SACAC,KACAC,KACAC,MACA;CACA,MAAMC,cAGA,CAAE;CACR,MAAM,MAAM,QAAQ,MAAM,SAAS,OAAO,QAAQ;CAClD,MAAM,QAAQ,KAAK,MAAM,MAAM,IAAI,GAAG,KAAK,KAAK,MAAM,IAAI,GAAG;AAC7D,MAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;EAC9B,MAAM,QAAQ,QACZ,OAAO,KAAK,KAAK,MAAM,IAAI,GAAG,IAC9B,YAAY,IAAI,EAAE,OACnB;AACD,cAAY,KAAK;GAAS;GAAO,MAAM,QAAQ;EAAI,EAAC;CACrD;AACD,QAAO;AACR"}
@@ -0,0 +1,15 @@
1
+ //#region src/components/Slider/type.d.ts
2
+ /**
3
+ * @module Slider
4
+ */
5
+ type ScaleType = 'mark' | 'mark-number' | 'number';
6
+ type ScaleOptions = ['step', ScaleType] | [number, ScaleType];
7
+ declare function generateOptionsList(options: ScaleOptions, min: number, max: number, step: number): {
8
+ value: number;
9
+ type: ScaleType;
10
+ }[];
11
+ //# sourceMappingURL=type.d.ts.map
12
+
13
+ //#endregion
14
+ export { ScaleOptions, ScaleType, generateOptionsList };
15
+ //# sourceMappingURL=type.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.cts","names":[],"sources":["../../../src/components/Slider/type.ts"],"sourcesContent":[],"mappings":";;AAMA;AACA;AAAwB,KADZ,SAAA,GACY,MAAA,GAAA,aAAA,GAAA,QAAA;AAAY,KAAxB,YAAA,GAAwB,CAAA,MAAA,EAAA,SAAA,CAAA,GAAA,CAAA,MAAA,EAAsB,SAAtB,CAAA;AAAsB,iBAE1C,mBAAA,CAF0C,OAAA,EAG/C,YAH+C,EAAA,GAAA,EAAA,MAAA,EAAA,GAAA,EAAA,MAAA,EAAA,IAAA,EAAA,MAAA,CAAA,EAAA;EAAS,KAAA,EAAA,MAAA;EAEnD,IAAA,EAQN,SARM;CAAmB,EAAA"}
@@ -0,0 +1,15 @@
1
+ //#region src/components/Slider/type.d.ts
2
+ /**
3
+ * @module Slider
4
+ */
5
+ type ScaleType = 'mark' | 'mark-number' | 'number';
6
+ type ScaleOptions = ['step', ScaleType] | [number, ScaleType];
7
+ declare function generateOptionsList(options: ScaleOptions, min: number, max: number, step: number): {
8
+ value: number;
9
+ type: ScaleType;
10
+ }[];
11
+ //# sourceMappingURL=type.d.ts.map
12
+
13
+ //#endregion
14
+ export { ScaleOptions, ScaleType, generateOptionsList };
15
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","names":[],"sources":["../../../src/components/Slider/type.ts"],"sourcesContent":[],"mappings":";;AAMA;AACA;AAAwB,KADZ,SAAA,GACY,MAAA,GAAA,aAAA,GAAA,QAAA;AAAY,KAAxB,YAAA,GAAwB,CAAA,MAAA,EAAA,SAAA,CAAA,GAAA,CAAA,MAAA,EAAsB,SAAtB,CAAA;AAAsB,iBAE1C,mBAAA,CAF0C,OAAA,EAG/C,YAH+C,EAAA,GAAA,EAAA,MAAA,EAAA,GAAA,EAAA,MAAA,EAAA,IAAA,EAAA,MAAA,CAAA,EAAA;EAAS,KAAA,EAAA,MAAA;EAEnD,IAAA,EAQN,SARM;CAAmB,EAAA"}
@@ -0,0 +1,35 @@
1
+ //#region src/math.ts
2
+ /**
3
+ *
4
+ * @category Math
5
+ */
6
+ function toFixed(x, fractionDigits) {
7
+ return Number(x.toFixed(fractionDigits));
8
+ }
9
+ /**
10
+ *
11
+ * @category Math
12
+ */
13
+ function decimalPart(x) {
14
+ return String(x).split(".")[1];
15
+ }
16
+
17
+ //#endregion
18
+ //#region src/components/Slider/type.ts
19
+ function generateOptionsList(options, min, max, step) {
20
+ const optionsList = [];
21
+ const per = options[0] == "step" ? step : options[0];
22
+ const count = Math.floor(max / per) - Math.ceil(min / per) + 1;
23
+ for (let i = 0; i < count; i++) {
24
+ const value = toFixed(per * (Math.ceil(min / per) + i), decimalPart(per)?.length);
25
+ optionsList.push({
26
+ value,
27
+ type: options[1]
28
+ });
29
+ }
30
+ return optionsList;
31
+ }
32
+
33
+ //#endregion
34
+ export { generateOptionsList };
35
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","names":["x: number","fractionDigits?: number","x: number | string","options: ScaleOptions","min: number","max: number","step: number","optionsList: {\n value: number\n type: ScaleType\n }[]"],"sources":["../../src/math.ts","../../src/components/Slider/type.ts"],"sourcesContent":["/**\n * clamp value between min and max\n * @category Math\n */\nexport function clamp(value: number, min: number, max: number) {\n return Math.max(min, Math.min(value, max))\n}\n\n/**\n * Normalize the value from 0 to 1\n * @category Math\n */\nexport function normalizeValue(\n rawValue: number,\n min: number,\n max: number,\n skew = 1,\n) {\n if (min >= max) throw new RangeError('requirements: min < max')\n const v = clamp((rawValue - min) / (max - min), 0, 1)\n return Math.pow(v, skew)\n}\n\n/**\n * Convert normalized values back to raw values.\n * @category Math\n */\nexport function rawValue(\n normalizedValue: number,\n min: number,\n max: number,\n skew = 1,\n) {\n if (min >= max) throw new RangeError('requirements: min < max')\n const v =\n skew == 1\n ? clamp(normalizedValue, 0, 1)\n : Math.exp(Math.log(clamp(normalizedValue, 0, 1)) / skew)\n return min + v * (max - min)\n}\n\n/**\n *\n * @category Math\n */\nexport function skewWithCenterValue(\n centerValue: number,\n min: number,\n max: number,\n) {\n if (!(min <= centerValue && centerValue <= max))\n throw new RangeError('requirements: min <= centerValue <= max')\n return Math.log(0.5) / Math.log((centerValue - min) / (max - min))\n}\n\n/**\n *\n * @category Math\n */\nexport function stepValue(value: number, step: number) {\n if (step <= 0) throw new RangeError('requirements: step > 0')\n const quotient = Math.floor(value / step)\n const decimalDigits = decimalPart(step)?.length\n const v = toFixed(quotient * step, decimalDigits)\n const next = toFixed((quotient + 1) * step, decimalDigits)\n return Math.abs(value - v) < Math.abs(value - next) ? v : next\n}\n\n/**\n *\n * @category Math\n */\nexport function toFixed(x: number, fractionDigits?: number) {\n return Number(x.toFixed(fractionDigits))\n}\n\n/**\n *\n * @category Math\n */\nexport function integerPart(x: number | string): string | undefined {\n if (Number.isNaN(x)) {\n return undefined\n }\n return String(x).split('.')[0]\n}\n\n/**\n *\n * @category Math\n */\nexport function decimalPart(x: number | string): string | undefined {\n return String(x).split('.')[1]\n}\n\n/**\n *\n * @category Math\n */\nexport function radian(degree: number) {\n return (Math.PI * degree) / 180\n}\n\n/**\n *\n * @category Math\n */\nexport function degree(radian: number) {\n return (180 * radian) / Math.PI\n}\n\n/**\n *\n * @category Math\n */\nexport function mapValue(\n value: number,\n inMin: number,\n inMax: number,\n outMin: number,\n outMax: number,\n) {\n return ((value - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin\n}\n\n/**\n *\n * @category Math\n */\nexport function dbToGain(db: number) {\n return Math.pow(10, db / 20)\n}\n\n/**\n *\n * @category Math\n */\nexport function gainToDb(gain: number) {\n return 20 * (Math.log(gain) / Math.LN10)\n}\n","/**\n * @module Slider\n */\n\nimport { decimalPart, toFixed } from '../../math'\n\nexport type ScaleType = 'mark' | 'mark-number' | 'number'\nexport type ScaleOptions = ['step', ScaleType] | [number, ScaleType]\n\nexport function generateOptionsList(\n options: ScaleOptions,\n min: number,\n max: number,\n step: number,\n) {\n const optionsList: {\n value: number\n type: ScaleType\n }[] = []\n const per = options[0] == 'step' ? step : options[0]\n const count = Math.floor(max / per) - Math.ceil(min / per) + 1\n for (let i = 0; i < count; i++) {\n const value = toFixed(\n per * (Math.ceil(min / per) + i),\n decimalPart(per)?.length,\n )\n optionsList.push({ value: value, type: options[1] })\n }\n return optionsList\n}\n"],"mappings":";;;;;AAwEA,SAAgB,QAAQA,GAAWC,gBAAyB;AAC1D,QAAO,OAAO,EAAE,QAAQ,eAAe,CAAC;AACzC;;;;;AAiBD,SAAgB,YAAYC,GAAwC;AAClE,QAAO,OAAO,EAAE,CAAC,MAAM,IAAI,CAAC;AAC7B;;;;ACpFD,SAAgB,oBACdC,SACAC,KACAC,KACAC,MACA;CACA,MAAMC,cAGA,CAAE;CACR,MAAM,MAAM,QAAQ,MAAM,SAAS,OAAO,QAAQ;CAClD,MAAM,QAAQ,KAAK,MAAM,MAAM,IAAI,GAAG,KAAK,KAAK,MAAM,IAAI,GAAG;AAC7D,MAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;EAC9B,MAAM,QAAQ,QACZ,OAAO,KAAK,KAAK,MAAM,IAAI,GAAG,IAC9B,YAAY,IAAI,EAAE,OACnB;AACD,cAAY,KAAK;GAAS;GAAO,MAAM,QAAQ;EAAI,EAAC;CACrD;AACD,QAAO;AACR"}
package/dist/index.cjs ADDED
@@ -0,0 +1,264 @@
1
+
2
+ //#region src/math.ts
3
+ /**
4
+ * clamp value between min and max
5
+ * @category Math
6
+ */
7
+ function clamp(value, min, max) {
8
+ return Math.max(min, Math.min(value, max));
9
+ }
10
+ /**
11
+ * Normalize the value from 0 to 1
12
+ * @category Math
13
+ */
14
+ function normalizeValue(rawValue$1, min, max, skew = 1) {
15
+ if (min >= max) throw new RangeError("requirements: min < max");
16
+ const v = clamp((rawValue$1 - min) / (max - min), 0, 1);
17
+ return Math.pow(v, skew);
18
+ }
19
+ /**
20
+ * Convert normalized values back to raw values.
21
+ * @category Math
22
+ */
23
+ function rawValue(normalizedValue, min, max, skew = 1) {
24
+ if (min >= max) throw new RangeError("requirements: min < max");
25
+ const v = skew == 1 ? clamp(normalizedValue, 0, 1) : Math.exp(Math.log(clamp(normalizedValue, 0, 1)) / skew);
26
+ return min + v * (max - min);
27
+ }
28
+ /**
29
+ *
30
+ * @category Math
31
+ */
32
+ function skewWithCenterValue(centerValue, min, max) {
33
+ if (!(min <= centerValue && centerValue <= max)) throw new RangeError("requirements: min <= centerValue <= max");
34
+ return Math.log(.5) / Math.log((centerValue - min) / (max - min));
35
+ }
36
+ /**
37
+ *
38
+ * @category Math
39
+ */
40
+ function stepValue(value, step) {
41
+ if (step <= 0) throw new RangeError("requirements: step > 0");
42
+ const quotient = Math.floor(value / step);
43
+ const decimalDigits = decimalPart(step)?.length;
44
+ const v = toFixed(quotient * step, decimalDigits);
45
+ const next = toFixed((quotient + 1) * step, decimalDigits);
46
+ return Math.abs(value - v) < Math.abs(value - next) ? v : next;
47
+ }
48
+ /**
49
+ *
50
+ * @category Math
51
+ */
52
+ function toFixed(x, fractionDigits) {
53
+ return Number(x.toFixed(fractionDigits));
54
+ }
55
+ /**
56
+ *
57
+ * @category Math
58
+ */
59
+ function integerPart(x) {
60
+ if (Number.isNaN(x)) return void 0;
61
+ return String(x).split(".")[0];
62
+ }
63
+ /**
64
+ *
65
+ * @category Math
66
+ */
67
+ function decimalPart(x) {
68
+ return String(x).split(".")[1];
69
+ }
70
+ /**
71
+ *
72
+ * @category Math
73
+ */
74
+ function radian(degree$1) {
75
+ return Math.PI * degree$1 / 180;
76
+ }
77
+ /**
78
+ *
79
+ * @category Math
80
+ */
81
+ function degree(radian$1) {
82
+ return 180 * radian$1 / Math.PI;
83
+ }
84
+ /**
85
+ *
86
+ * @category Math
87
+ */
88
+ function mapValue(value, inMin, inMax, outMin, outMax) {
89
+ return (value - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
90
+ }
91
+ /**
92
+ *
93
+ * @category Math
94
+ */
95
+ function dbToGain(db) {
96
+ return Math.pow(10, db / 20);
97
+ }
98
+ /**
99
+ *
100
+ * @category Math
101
+ */
102
+ function gainToDb(gain) {
103
+ return 20 * (Math.log(gain) / Math.LN10);
104
+ }
105
+
106
+ //#endregion
107
+ //#region src/util.ts
108
+ function styleHelper(value, op, influencer) {
109
+ if (op && influencer) if (typeof value == "number") {
110
+ if (op == "+") return `${value + influencer}px`;
111
+ if (op == "-") return `${value - influencer}px`;
112
+ if (op == "*") return `${value * influencer}px`;
113
+ if (op == "/") return `${value / influencer}px`;
114
+ } else return `calc(${value}px ${op} ${influencer})`;
115
+ else if (typeof value == "number") return `${value}px`;
116
+ else return value;
117
+ }
118
+ /**
119
+ * @category Utility
120
+ */
121
+ function isEmpty(obj) {
122
+ return Object.keys(obj).length == 0;
123
+ }
124
+ /**
125
+ * @category Utility
126
+ */
127
+ function mod(n, m) {
128
+ return (n % m + m) % m;
129
+ }
130
+ /**
131
+ * @category Utility
132
+ */
133
+ function xor(a = false, b = false) {
134
+ return (a || b) && a != b;
135
+ }
136
+
137
+ //#endregion
138
+ //#region src/midi.ts
139
+ /**
140
+ * @category MIDI
141
+ */
142
+ const whiteKeys = [
143
+ "A",
144
+ "B",
145
+ "C",
146
+ "D",
147
+ "E",
148
+ "F",
149
+ "G"
150
+ ];
151
+ /**
152
+ * @category MIDI
153
+ */
154
+ const noteKeys = [
155
+ "C",
156
+ "C#",
157
+ "D",
158
+ "D#",
159
+ "E",
160
+ "F",
161
+ "F#",
162
+ "G",
163
+ "G#",
164
+ "A",
165
+ "A#",
166
+ "B"
167
+ ];
168
+ /**
169
+ * @category MIDI
170
+ */
171
+ function parseNoteName(noteName$1) {
172
+ const m = noteName$1.match(/^([a-g])(#{0,2}|b{0,2})(-?\d+)$/i);
173
+ if (!m) throw new Error("Invalid note name");
174
+ const [, letter, accidental, octave] = m;
175
+ return {
176
+ letter: letter.toLocaleUpperCase(),
177
+ accidental,
178
+ octave: Number(octave)
179
+ };
180
+ }
181
+ /**
182
+ * Convert noteName to noteNumber
183
+ * @category MIDI
184
+ */
185
+ function noteNumber(noteName$1) {
186
+ const { letter, accidental, octave } = parseNoteName(noteName$1);
187
+ const noteIndex = noteKeys.indexOf(letter.toLocaleUpperCase());
188
+ const accidentalValue = (accidental[0] == "b" ? -1 : 1) * accidental.length;
189
+ return noteIndex + 12 * (Number(octave) + 1) + accidentalValue;
190
+ }
191
+ /**
192
+ * Convert noteNumber to noteName
193
+ * C-1 = 0
194
+ * G9 = 127
195
+ * @param noteNumber noteNumber
196
+ * @category MIDI
197
+ */
198
+ function noteName(noteNumber$1) {
199
+ const noteIndex = mod(noteNumber$1, 12);
200
+ const octave = Math.floor(noteNumber$1 / 12) - 1;
201
+ return `${noteKeys[noteIndex]}${octave}`;
202
+ }
203
+ /**
204
+ * Convert noteNumber to noteKey
205
+ * @category MIDI
206
+ */
207
+ function noteKey(noteNumber$1) {
208
+ return noteKeys[mod(noteNumber$1, 12)];
209
+ }
210
+ /**
211
+ * @param note noteNumber: 0 ~ 127 or noteName e.g. 'C3'
212
+ * @category MIDI
213
+ */
214
+ function isWhiteKey(note) {
215
+ const n = typeof note == "string" ? noteNumber(note) : note;
216
+ return mod(n, 12) == 0 || mod(n, 12) == 2 || mod(n, 12) == 4 || mod(n, 12) == 5 || mod(n, 12) == 7 || mod(n, 12) == 9 || mod(n, 12) == 11;
217
+ }
218
+ /**
219
+ * @param note noteNumber: 0 ~ 127 or noteName e.g. 'C3'
220
+ * @category MIDI
221
+ */
222
+ function isBlackKey(note) {
223
+ return !isWhiteKey(note);
224
+ }
225
+ /**
226
+ * @param note noteNumber: 0 ~ 127 or noteName e.g. 'C3'
227
+ * @param detune [cent]
228
+ * @param a4 A4 frequency [Hz]
229
+ * @returns frequency [Hz]
230
+ * @category MIDI
231
+ */
232
+ function noteToFrequency(note, detune = 0, a4 = 440) {
233
+ const n = typeof note == "string" ? noteNumber(note) : note;
234
+ return a4 / 32 * 2 ** ((n - 9 + detune / 100) / 12);
235
+ }
236
+
237
+ //#endregion
238
+ exports.clamp = clamp;
239
+ exports.dbToGain = dbToGain;
240
+ exports.decimalPart = decimalPart;
241
+ exports.degree = degree;
242
+ exports.gainToDb = gainToDb;
243
+ exports.integerPart = integerPart;
244
+ exports.isBlackKey = isBlackKey;
245
+ exports.isEmpty = isEmpty;
246
+ exports.isWhiteKey = isWhiteKey;
247
+ exports.mapValue = mapValue;
248
+ exports.mod = mod;
249
+ exports.normalizeValue = normalizeValue;
250
+ exports.noteKey = noteKey;
251
+ exports.noteKeys = noteKeys;
252
+ exports.noteName = noteName;
253
+ exports.noteNumber = noteNumber;
254
+ exports.noteToFrequency = noteToFrequency;
255
+ exports.parseNoteName = parseNoteName;
256
+ exports.radian = radian;
257
+ exports.rawValue = rawValue;
258
+ exports.skewWithCenterValue = skewWithCenterValue;
259
+ exports.stepValue = stepValue;
260
+ exports.styleHelper = styleHelper;
261
+ exports.toFixed = toFixed;
262
+ exports.whiteKeys = whiteKeys;
263
+ exports.xor = xor;
264
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["value: number","min: number","max: number","rawValue: number","rawValue","normalizedValue: number","centerValue: number","step: number","x: number","fractionDigits?: number","x: number | string","degree: number","degree","radian: number","radian","inMin: number","inMax: number","outMin: number","outMax: number","db: number","gain: number","value: string | number","op?: Operator","influencer?: number","obj: object","n: number","m: number","noteName: string","noteName","noteNumber: number","noteNumber","note: number | string"],"sources":["../src/math.ts","../src/util.ts","../src/midi.ts"],"sourcesContent":["/**\n * clamp value between min and max\n * @category Math\n */\nexport function clamp(value: number, min: number, max: number) {\n return Math.max(min, Math.min(value, max))\n}\n\n/**\n * Normalize the value from 0 to 1\n * @category Math\n */\nexport function normalizeValue(\n rawValue: number,\n min: number,\n max: number,\n skew = 1,\n) {\n if (min >= max) throw new RangeError('requirements: min < max')\n const v = clamp((rawValue - min) / (max - min), 0, 1)\n return Math.pow(v, skew)\n}\n\n/**\n * Convert normalized values back to raw values.\n * @category Math\n */\nexport function rawValue(\n normalizedValue: number,\n min: number,\n max: number,\n skew = 1,\n) {\n if (min >= max) throw new RangeError('requirements: min < max')\n const v =\n skew == 1\n ? clamp(normalizedValue, 0, 1)\n : Math.exp(Math.log(clamp(normalizedValue, 0, 1)) / skew)\n return min + v * (max - min)\n}\n\n/**\n *\n * @category Math\n */\nexport function skewWithCenterValue(\n centerValue: number,\n min: number,\n max: number,\n) {\n if (!(min <= centerValue && centerValue <= max))\n throw new RangeError('requirements: min <= centerValue <= max')\n return Math.log(0.5) / Math.log((centerValue - min) / (max - min))\n}\n\n/**\n *\n * @category Math\n */\nexport function stepValue(value: number, step: number) {\n if (step <= 0) throw new RangeError('requirements: step > 0')\n const quotient = Math.floor(value / step)\n const decimalDigits = decimalPart(step)?.length\n const v = toFixed(quotient * step, decimalDigits)\n const next = toFixed((quotient + 1) * step, decimalDigits)\n return Math.abs(value - v) < Math.abs(value - next) ? v : next\n}\n\n/**\n *\n * @category Math\n */\nexport function toFixed(x: number, fractionDigits?: number) {\n return Number(x.toFixed(fractionDigits))\n}\n\n/**\n *\n * @category Math\n */\nexport function integerPart(x: number | string): string | undefined {\n if (Number.isNaN(x)) {\n return undefined\n }\n return String(x).split('.')[0]\n}\n\n/**\n *\n * @category Math\n */\nexport function decimalPart(x: number | string): string | undefined {\n return String(x).split('.')[1]\n}\n\n/**\n *\n * @category Math\n */\nexport function radian(degree: number) {\n return (Math.PI * degree) / 180\n}\n\n/**\n *\n * @category Math\n */\nexport function degree(radian: number) {\n return (180 * radian) / Math.PI\n}\n\n/**\n *\n * @category Math\n */\nexport function mapValue(\n value: number,\n inMin: number,\n inMax: number,\n outMin: number,\n outMax: number,\n) {\n return ((value - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin\n}\n\n/**\n *\n * @category Math\n */\nexport function dbToGain(db: number) {\n return Math.pow(10, db / 20)\n}\n\n/**\n *\n * @category Math\n */\nexport function gainToDb(gain: number) {\n return 20 * (Math.log(gain) / Math.LN10)\n}\n","type Operator = '+' | '-' | '*' | '/'\n\n/**\n * @category Utility\n */\nexport function styleHelper(value: string | number): string\nexport function styleHelper(\n value: string | number,\n op: Operator,\n influencer?: number,\n): string\nexport function styleHelper(\n value: string | number,\n op?: Operator,\n influencer?: number,\n) {\n if (op && influencer) {\n if (typeof value == 'number') {\n if (op == '+') return `${value + influencer}px`\n if (op == '-') return `${value - influencer}px`\n if (op == '*') return `${value * influencer}px`\n if (op == '/') return `${value / influencer}px`\n } else {\n return `calc(${value}px ${op} ${influencer})`\n }\n } else {\n if (typeof value == 'number') {\n return `${value}px`\n } else {\n return value\n }\n }\n}\n\n/**\n * @category Utility\n */\nexport function isEmpty(obj: object) {\n return Object.keys(obj).length == 0\n}\n\n/**\n * @category Utility\n */\nexport function mod(n: number, m: number) {\n return ((n % m) + m) % m\n}\n\n/**\n * @category Utility\n */\nexport function xor(a = false, b = false) {\n return (a || b) && a != b\n}\n","import { mod } from './util'\n\n/**\n * @category MIDI\n */\nexport const whiteKeys = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] as const\n\n/**\n * @category MIDI\n */\nexport type WhiteKey = (typeof whiteKeys)[number]\n\n/**\n * @category MIDI\n */\nexport const noteKeys = [\n 'C',\n 'C#',\n 'D',\n 'D#',\n 'E',\n 'F',\n 'F#',\n 'G',\n 'G#',\n 'A',\n 'A#',\n 'B',\n] as const\n\n/**\n * @category MIDI\n */\nexport type NoteKey = (typeof noteKeys)[number]\n\n/**\n * @category MIDI\n */\nexport function parseNoteName(noteName: string) {\n const m = noteName.match(/^([a-g])(#{0,2}|b{0,2})(-?\\d+)$/i)\n if (!m) throw new Error('Invalid note name')\n const [, letter, accidental, octave] = m\n return {\n letter: letter.toLocaleUpperCase() as WhiteKey,\n accidental: accidental as '#' | '##' | 'b' | 'bb' | '',\n octave: Number(octave),\n }\n}\n\n/**\n * Convert noteName to noteNumber\n * @category MIDI\n */\nexport function noteNumber(noteName: string) {\n const { letter, accidental, octave } = parseNoteName(noteName)\n const noteIndex = noteKeys.indexOf(letter.toLocaleUpperCase() as NoteKey)\n const accidentalValue = (accidental[0] == 'b' ? -1 : 1) * accidental.length\n return noteIndex + 12 * (Number(octave) + 1) + accidentalValue\n}\n\n/**\n * Convert noteNumber to noteName\n * C-1 = 0\n * G9 = 127\n * @param noteNumber noteNumber\n * @category MIDI\n */\nexport function noteName(noteNumber: number): `${NoteKey}${number}` {\n const noteIndex = mod(noteNumber, 12)\n const octave = Math.floor(noteNumber / 12) - 1\n return `${noteKeys[noteIndex]}${octave}`\n}\n\n/**\n * Convert noteNumber to noteKey\n * @category MIDI\n */\nexport function noteKey(noteNumber: number): NoteKey {\n return noteKeys[mod(noteNumber, 12)]\n}\n\n/**\n * @param note noteNumber: 0 ~ 127 or noteName e.g. 'C3'\n * @category MIDI\n */\nexport function isWhiteKey(note: number | string) {\n const n = typeof note == 'string' ? noteNumber(note) : note\n return (\n mod(n, 12) == 0 ||\n mod(n, 12) == 2 ||\n mod(n, 12) == 4 ||\n mod(n, 12) == 5 ||\n mod(n, 12) == 7 ||\n mod(n, 12) == 9 ||\n mod(n, 12) == 11\n )\n}\n\n/**\n * @param note noteNumber: 0 ~ 127 or noteName e.g. 'C3'\n * @category MIDI\n */\nexport function isBlackKey(note: number | string) {\n return !isWhiteKey(note)\n}\n\n/**\n * @param note noteNumber: 0 ~ 127 or noteName e.g. 'C3'\n * @param detune [cent]\n * @param a4 A4 frequency [Hz]\n * @returns frequency [Hz]\n * @category MIDI\n */\nexport function noteToFrequency(note: number | string, detune = 0, a4 = 440) {\n const n = typeof note == 'string' ? noteNumber(note) : note\n return (a4 / 32) * 2 ** ((n - 9 + detune / 100) / 12)\n}\n"],"mappings":";;;;;;AAIA,SAAgB,MAAMA,OAAeC,KAAaC,KAAa;AAC7D,QAAO,KAAK,IAAI,KAAK,KAAK,IAAI,OAAO,IAAI,CAAC;AAC3C;;;;;AAMD,SAAgB,eACdC,YACAF,KACAC,KACA,OAAO,GACP;AACA,KAAI,OAAO,IAAK,OAAM,IAAI,WAAW;CACrC,MAAM,IAAI,OAAOE,aAAW,QAAQ,MAAM,MAAM,GAAG,EAAE;AACrD,QAAO,KAAK,IAAI,GAAG,KAAK;AACzB;;;;;AAMD,SAAgB,SACdC,iBACAJ,KACAC,KACA,OAAO,GACP;AACA,KAAI,OAAO,IAAK,OAAM,IAAI,WAAW;CACrC,MAAM,IACJ,QAAQ,IACJ,MAAM,iBAAiB,GAAG,EAAE,GAC5B,KAAK,IAAI,KAAK,IAAI,MAAM,iBAAiB,GAAG,EAAE,CAAC,GAAG,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