@choksheak/ts-utils 0.2.4 → 0.2.5

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@choksheak/ts-utils",
3
3
  "license": "The Unlicense",
4
- "version": "0.2.4",
4
+ "version": "0.2.5",
5
5
  "description": "Random Typescript utilities with support for full tree-shaking",
6
6
  "private": false,
7
7
  "scripts": {
package/round.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Simplifies rounding of floating point numbers. Note that if your number ends
3
+ * with zeros, and you convert the number to string, you will lose the zeroes
4
+ * at the end. To show the exact number of decimal places, you'll need to use
5
+ * toFixed(). E.g. round(1.20, 2).toFixed(2).
6
+ */
7
+ export declare function round(n: number, numDecimalPlaces?: number): number;
8
+ /**
9
+ * Returns a string with the number in the exact number of decimal places
10
+ * specified, in case the number ends with zeroes.
11
+ */
12
+ export declare function roundS(n: number, numDecimalPlaces?: number): string;
package/round.js ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/round.ts
21
+ var round_exports = {};
22
+ __export(round_exports, {
23
+ round: () => round,
24
+ roundS: () => roundS
25
+ });
26
+ module.exports = __toCommonJS(round_exports);
27
+ function round(n, numDecimalPlaces = 0) {
28
+ const multipler = Math.pow(10, numDecimalPlaces);
29
+ return Math.round(n * multipler) / multipler;
30
+ }
31
+ function roundS(n, numDecimalPlaces = 0) {
32
+ return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
33
+ }
34
+ // Annotate the CommonJS export names for ESM import in node:
35
+ 0 && (module.exports = {
36
+ round,
37
+ roundS
38
+ });
39
+ //# sourceMappingURL=round.js.map
package/round.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/round.ts"],"sourcesContent":["/**\n * Simplifies rounding of floating point numbers. Note that if your number ends\n * with zeros, and you convert the number to string, you will lose the zeroes\n * at the end. To show the exact number of decimal places, you'll need to use\n * toFixed(). E.g. round(1.20, 2).toFixed(2).\n */\nexport function round(n: number, numDecimalPlaces = 0): number {\n const multipler = Math.pow(10, numDecimalPlaces);\n return Math.round(n * multipler) / multipler;\n}\n\n/**\n * Returns a string with the number in the exact number of decimal places\n * specified, in case the number ends with zeroes.\n */\nexport function roundS(n: number, numDecimalPlaces = 0): string {\n return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMO,SAAS,MAAM,GAAW,mBAAmB,GAAW;AAC7D,QAAM,YAAY,KAAK,IAAI,IAAI,gBAAgB;AAC/C,SAAO,KAAK,MAAM,IAAI,SAAS,IAAI;AACrC;AAMO,SAAS,OAAO,GAAW,mBAAmB,GAAW;AAC9D,SAAO,MAAM,GAAG,gBAAgB,EAAE,QAAQ,gBAAgB;AAC5D;","names":[]}
package/src/round.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Simplifies rounding of floating point numbers. Note that if your number ends
3
+ * with zeros, and you convert the number to string, you will lose the zeroes
4
+ * at the end. To show the exact number of decimal places, you'll need to use
5
+ * toFixed(). E.g. round(1.20, 2).toFixed(2).
6
+ */
7
+ export function round(n: number, numDecimalPlaces = 0): number {
8
+ const multipler = Math.pow(10, numDecimalPlaces);
9
+ return Math.round(n * multipler) / multipler;
10
+ }
11
+
12
+ /**
13
+ * Returns a string with the number in the exact number of decimal places
14
+ * specified, in case the number ends with zeroes.
15
+ */
16
+ export function roundS(n: number, numDecimalPlaces = 0): string {
17
+ return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
18
+ }