@swissgeo/numbers 1.0.0-beta.1

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.
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Compute the circular mean of radians angles
3
+ *
4
+ * @param values List of radians angles to compute the circular mean
5
+ * @returns Circular mean in radians
6
+ * @see https://en.wikipedia.org/wiki/Circular_mean
7
+ */
8
+ export declare function circularMean(values: number[]): number | undefined;
9
+
10
+ /**
11
+ * @param value A floating number
12
+ * @param fromList A list of integer
13
+ * @returns The closest value, from the list of integer, matching the floating number (will floor or
14
+ * ceil accordingly)
15
+ */
16
+ export declare function closest(value: number, fromList: number[]): number;
17
+
18
+ /**
19
+ * Format this number into a string with the `de-CH` locale (thousands separator is ')
20
+ *
21
+ * @param value The value to be formatted
22
+ * @param decimal How many decimals should be shown
23
+ */
24
+ export declare function format(value: number, decimal?: number): string;
25
+
26
+ /**
27
+ * Returns a string representing a number with the right thousand separator
28
+ *
29
+ * @param num A number, or a string representing a number
30
+ * @param separator The thousand separator, default to "'"
31
+ * @returns String A formatted string representing a number, e.g. 1'546
32
+ */
33
+ export declare function formatThousand(num: number | string, separator?: string): string;
34
+
35
+ export declare interface GeoadminNumberUtils {
36
+ round: typeof round;
37
+ closest: typeof closest;
38
+ isNumber: typeof isNumber;
39
+ randomIntBetween: typeof randomIntBetween;
40
+ format: typeof format;
41
+ formatThousand: typeof formatThousand;
42
+ wrapDegrees: typeof wrapDegrees;
43
+ isTimestampYYYYMMDD: typeof isTimestampYYYYMMDD;
44
+ circularMean: typeof circularMean;
45
+ }
46
+
47
+ /**
48
+ * Returns true if value represents or is a number (a string containing a valid number will return
49
+ * true)
50
+ */
51
+ export declare function isNumber(value: unknown): boolean;
52
+
53
+ /**
54
+ * Test if the given string match a timestamp of the format YYYYMMDD
55
+ *
56
+ * NOTE: it only supports timestamp from 00000101 to 99991231
57
+ *
58
+ * @param timestamp
59
+ * @returns True if it match false otherwise
60
+ */
61
+ export declare function isTimestampYYYYMMDD(timestamp: string): boolean;
62
+
63
+ declare const numbers: GeoadminNumberUtils;
64
+ export default numbers;
65
+ export { numbers }
66
+
67
+ /**
68
+ * Returns a random int in the range provided
69
+ *
70
+ * @param start The start of the range
71
+ * @param end The end of the range, must be greater than the start
72
+ * @returns A random number between start and end (both included), or 0 if either start or end is
73
+ * not a number or start is bigger than the end
74
+ */
75
+ export declare function randomIntBetween(start: number, end: number): number;
76
+
77
+ /** @module swissgeo/numbers */
78
+ /**
79
+ * Rounds the given value according to how many decimals are wanted
80
+ *
81
+ * @param value
82
+ * @param decimals How many decimals after the separator must be present after rounding (default to
83
+ * 0)
84
+ * @returns Value rounded
85
+ */
86
+ export declare function round(value: number, decimals?: number): number;
87
+
88
+ /**
89
+ * Makes sure that the given angle stays between -359.99999 and 359.9999 degrees (will start over at
90
+ * 0 whenever a full-circle is present)
91
+ */
92
+ export declare function wrapDegrees(angleInDegrees: number): number;
93
+
94
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ function s(n, r = 0) {
2
+ if (!i(n))
3
+ return Number.NaN;
4
+ if (r === 0)
5
+ return Math.round(n);
6
+ const t = Math.pow(10, r);
7
+ return Math.round(n * t) / t;
8
+ }
9
+ function f(n, r) {
10
+ if (Array.isArray(r) && r.length > 2) {
11
+ const t = r.map((u) => Math.abs(n - u)), e = t.reduce(
12
+ (u, c) => u > c ? c : u
13
+ ), o = t.indexOf(e);
14
+ return typeof r[o] != "number" || isNaN(r[o]) ? n : r[o];
15
+ }
16
+ return n;
17
+ }
18
+ function i(n) {
19
+ return n != null && !Number.isNaN(Number(n)) && (typeof n != "string" || n.length !== 0);
20
+ }
21
+ function h(n, r) {
22
+ return !Number.isInteger(n) || !Number.isInteger(r) || r < n ? 0 : Math.floor(Math.random() * (r - n + 1) + n);
23
+ }
24
+ const a = /\B(?=(\d{3})+(?!\d))/g;
25
+ function p(n, r = 2) {
26
+ return i(n) ? `${s(n, r)}`.replace(a, "'") : "";
27
+ }
28
+ function d(n, r = "'") {
29
+ const t = ".", e = `${n}`.split(t);
30
+ return typeof e[0] != "string" || e[0].length === 0 ? `${n}` : (e[0] = e[0].replace(a, r), e.join(t));
31
+ }
32
+ function m(n) {
33
+ const r = Math.sign(n), t = Math.abs(n);
34
+ return t > 360 ? r * (t % 360) : t === 360 ? 0 : n;
35
+ }
36
+ function M(n) {
37
+ return /^\d{4}(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])$/.test(n);
38
+ }
39
+ function N(n) {
40
+ if (!Array.isArray(n) || n.some((o) => !i(o)))
41
+ return;
42
+ const r = n.reduce((o, u) => o + Math.cos(u), 0), t = n.reduce((o, u) => o + Math.sin(u), 0);
43
+ let e = Math.atan2(t, r);
44
+ return e < 0 && (e = 6.2831853 + e), e;
45
+ }
46
+ const b = {
47
+ round: s,
48
+ closest: f,
49
+ isNumber: i,
50
+ randomIntBetween: h,
51
+ format: p,
52
+ formatThousand: d,
53
+ wrapDegrees: m,
54
+ isTimestampYYYYMMDD: M,
55
+ circularMean: N
56
+ };
57
+ export {
58
+ N as circularMean,
59
+ f as closest,
60
+ b as default,
61
+ p as format,
62
+ d as formatThousand,
63
+ i as isNumber,
64
+ M as isTimestampYYYYMMDD,
65
+ b as numbers,
66
+ h as randomIntBetween,
67
+ s as round,
68
+ m as wrapDegrees
69
+ };
@@ -0,0 +1 @@
1
+ (function(t,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(t=typeof globalThis<"u"?globalThis:t||self,s(t["@swissgeo/numbers"]={}))})(this,function(t){"use strict";function s(n,e=0){if(!c(n))return Number.NaN;if(e===0)return Math.round(n);const r=Math.pow(10,e);return Math.round(n*r)/r}function a(n,e){if(Array.isArray(e)&&e.length>2){const r=e.map(o=>Math.abs(n-o)),i=r.reduce((o,N)=>o>N?N:o),u=r.indexOf(i);return typeof e[u]!="number"||isNaN(e[u])?n:e[u]}return n}function c(n){return n!=null&&!Number.isNaN(Number(n))&&(typeof n!="string"||n.length!==0)}function f(n,e){return!Number.isInteger(n)||!Number.isInteger(e)||e<n?0:Math.floor(Math.random()*(e-n+1)+n)}const d=/\B(?=(\d{3})+(?!\d))/g;function m(n,e=2){return c(n)?`${s(n,e)}`.replace(d,"'"):""}function h(n,e="'"){const r=".",i=`${n}`.split(r);return typeof i[0]!="string"||i[0].length===0?`${n}`:(i[0]=i[0].replace(d,e),i.join(r))}function M(n){const e=Math.sign(n),r=Math.abs(n);return r>360?e*(r%360):r===360?0:n}function b(n){return/^\d{4}(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])$/.test(n)}function g(n){if(!Array.isArray(n)||n.some(u=>!c(u)))return;const e=n.reduce((u,o)=>u+Math.cos(o),0),r=n.reduce((u,o)=>u+Math.sin(o),0);let i=Math.atan2(r,e);return i<0&&(i=6.2831853+i),i}const p={round:s,closest:a,isNumber:c,randomIntBetween:f,format:m,formatThousand:h,wrapDegrees:M,isTimestampYYYYMMDD:b,circularMean:g};t.circularMean=g,t.closest=a,t.default=p,t.format=m,t.formatThousand=h,t.isNumber=c,t.isTimestampYYYYMMDD=b,t.numbers=p,t.randomIntBetween=f,t.round=s,t.wrapDegrees=M,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@swissgeo/numbers",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "Numbers utils for SWISSGEO projects.",
5
+ "license": "BSD-3-Clause",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.umd.cjs"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "pnpm run type-check && pnpm run generate-types && vite build",
19
+ "build:dev": "pnpm run build --mode development",
20
+ "build:dev:watch": "pnpm run build --watch --mode development",
21
+ "build:int": "pnpm run build --mode integration",
22
+ "build:prod": "pnpm run build --mode production",
23
+ "dev": "vite",
24
+ "generate-types": "tsc --declaration",
25
+ "lint": "eslint --fix",
26
+ "lint:no-fix": "eslint",
27
+ "test:unit": "vitest --run --mode development --environment jsdom",
28
+ "test:unit:watch": "vitest --mode development --environment jsdom",
29
+ "type-check": "tsc -p tsconfig.json"
30
+ },
31
+ "dependencies": {
32
+ "@swissgeo/log": "workspace:*"
33
+ },
34
+ "devDependencies": {
35
+ "@microsoft/api-extractor": "catalog:",
36
+ "@prettier/plugin-xml": "catalog:",
37
+ "@swissgeo/config-eslint": "workspace:*",
38
+ "@swissgeo/config-prettier": "workspace:*",
39
+ "@swissgeo/config-typescript": "workspace:*",
40
+ "@types/chai": "catalog:",
41
+ "chai": "catalog:",
42
+ "eslint": "catalog:",
43
+ "prettier": "catalog:",
44
+ "prettier-plugin-jsdoc": "catalog:",
45
+ "prettier-plugin-packagejson": "catalog:",
46
+ "prettier-plugin-tailwindcss": "catalog:",
47
+ "typescript": "catalog:",
48
+ "unplugin-dts": "catalog:",
49
+ "vite": "catalog:",
50
+ "vitest": "catalog:"
51
+ }
52
+ }