Skip to content

Commit 8dad13e

Browse files
authored
Merge pull request League-of-Foundry-Developers#2192 from Spice-King/v10/common-utils
Update common/utils and common/primitives to v10.277
2 parents d82c586 + f82f9cf commit 8dad13e

23 files changed

+807
-364
lines changed

src/foundry/common/module.mjs.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './types.mjs';
2-
import './utils/primitives.mjs';
2+
import './primitives/module.mjs';
33
import * as _CONST from './constants.mjs';
44
import * as _abstract from './abstract/module.mjs';
55
import * as _data from './data/module.mjs';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
declare namespace Array {
2+
type Flattened<T> = T extends Array<infer U> ? Flattened<U> : T;
3+
}
4+
5+
interface Array<T> {
6+
/**
7+
* Flatten nested arrays by concatenating their contents
8+
* @returns An array containing the concatenated inner values
9+
*/
10+
deepFlatten(): Array<Array.Flattened<T>>;
11+
12+
/**
13+
* Test element-wise equality of the values of this array against the values of another array
14+
* @param other - Some other array against which to test equality
15+
* @returns Are the two arrays element-wise equal?
16+
*/
17+
equals(other: T[]): boolean;
18+
19+
/**
20+
* Partition an original array into two children array based on a logical test
21+
* Elements which test as false go into the first result while elements testing as true appear in the second
22+
* @param rule - The rule to partition by
23+
* @returns An Array of length two whose elements are the partitioned pieces of the original
24+
*/
25+
partition<S extends T>(rule: (val: T) => val is S): [Exclude<T, S>[], S[]];
26+
partition(rule: (val: T) => boolean): [T[], T[]];
27+
28+
/**
29+
* Join an Array using a string separator, first filtering out any parts which return a false-y value
30+
* @param sep - The separator string
31+
* @returns The joined string, filtered of any false values
32+
*/
33+
filterJoin(sep: string): string;
34+
35+
/**
36+
* Find an element within the Array and remove it from the array
37+
* @param find - A function to use as input to findIndex
38+
* @param replace - A replacement for the spliced element
39+
* @returns The replacement element, the removed element, or null if no element was found.
40+
*/
41+
findSplice(find: (value: T, index: number, obj: T[]) => boolean, replace?: T): T | null;
42+
}
43+
44+
interface ArrayConstructor {
45+
/**
46+
* Create and initialize an array of length n with integers from 0 to n-1
47+
* @param n - The desired array length
48+
* @param min - A desired minimum number from which the created array starts (default: `0`)
49+
* @returns An array of integers from min to min+n
50+
*/
51+
fromRange(n: number, min?: number): number[];
52+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface Date {
2+
/**
3+
* Test whether a Date instance is valid.
4+
* A valid date returns a number for its timestamp, and NaN otherwise.
5+
* NaN is never equal to itself.
6+
*/
7+
isValid(): boolean;
8+
9+
/**
10+
* Return a standard YYYY-MM-DD string for the Date instance.
11+
* @returns The date in YYYY-MM-DD format
12+
*/
13+
toDateInputString(): string;
14+
15+
/**
16+
* Return a standard H:M:S.Z string for the Date instance.
17+
* @returns The time in H:M:S format
18+
*/
19+
toTimeInputString(): string;
20+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
interface Math {
2+
/**
3+
* Bound a number between some minimum and maximum value, inclusively
4+
* @param num - The current value
5+
* @param min - The minimum allowed value
6+
* @param max - The maximum allowed value
7+
* @returns The clamped number
8+
*/
9+
clamped(num: number, min: number, max: number): number;
10+
11+
/**
12+
* Linear interpolation function
13+
* @param a - An initial value when weight is 0.
14+
* @param b - A terminal value when weight is 1.
15+
* @param w - A weight between 0 and 1.
16+
* @returns The interpolated value between a and b with weight w.
17+
*/
18+
mix(a: number, b: number, w: number): number;
19+
20+
/**
21+
* Transform an angle in degrees to be bounded within the domain [0, 360]
22+
* @param degrees - An angle in degrees
23+
* @param base - The base angle to normalize to, either 0 for [0, 360) or 360 for (0, 360] (default: `0`)
24+
* @returns The same angle on the range [0, 360) or (0, 360]
25+
*/
26+
normalizeDegrees(degrees: number, base?: number): number;
27+
28+
/**
29+
* Transform an angle in radians to be bounded within the domain [-PI, PI]
30+
* @param radians - An angle in degrees
31+
* @returns The same angle on the range [-PI, PI]
32+
*/
33+
normalizeRadians(radians: number): number;
34+
35+
/**
36+
* Round a floating point number to a certain number of decimal places
37+
* @param number - A floating point number
38+
* @param places - An integer number of decimal places
39+
*/
40+
roundDecimals(number: number, places: number): number;
41+
42+
/**
43+
* Round a floating-point number using the fastest available method.
44+
* This should be used instead of Math.round in cases where performance matters.
45+
* A key limitation is this method returns zero if the input is NaN or undefined.
46+
* @param number - A finite number
47+
* @returns The rounded number
48+
*/
49+
roundFast(number: number): number;
50+
51+
/**
52+
* Transform an angle in radians to a number in degrees
53+
* @param angle - An angle in radians
54+
* @returns An angle in degrees
55+
*/
56+
toDegrees(angle: number): number;
57+
58+
/**
59+
* Transform an angle in degrees to an angle in radians
60+
* @param angle - An angle in degrees
61+
* @returns An angle in radians
62+
*/
63+
toRadians(angle: number): number;
64+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import './array.mjs';
2+
import './date.mjs';
3+
import './math.mjs';
4+
import './number.mjs';
5+
import './regex.mjs';
6+
import './set.mjs';
7+
import './string.mjs';
8+
import './url.mjs';
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
interface Number {
2+
/**
3+
* Test for near-equivalence of two numbers within some permitted epsilon
4+
* @param n - Some other number
5+
* @param e - Some permitted epsilon, by default 1e-8
6+
(default: `1e-8`)
7+
* @returns Are the numbers almost equal?
8+
*/
9+
almostEqual(n: number, e?: number): boolean;
10+
11+
/**
12+
* Transform a number to an ordinal string representation. i.e.
13+
* ```
14+
* 1 => 1st
15+
* 2 => 2nd
16+
* 3 => 3rd
17+
* ```
18+
*/
19+
ordinalString(): string;
20+
21+
/**
22+
* Return a string front-padded by zeroes to reach a certain number of numeral characters
23+
* @param digits - The number of characters desired
24+
* @returns The zero-padded number
25+
*/
26+
paddedString(digits: number): string;
27+
28+
/**
29+
* Return a string prefaced by the sign of the number (+) or (-)
30+
* @returns The signed number as a string
31+
*/
32+
signedString(): string;
33+
34+
/**
35+
* Round a number to the nearest number which is a multiple of a given interval
36+
* @param interval - The interval to round the number to the nearest multiple of
37+
* (default: `1`)
38+
* @param method - The rounding method in: round, ceil, floor
39+
* (default: `'round'`)
40+
* @returns The rounded number
41+
*
42+
* @example Round a number to the nearest step interval
43+
* ```typescript
44+
* let n = 17.18;
45+
* n.toNearest(5); // 15
46+
* n.toNearest(10); // 20
47+
* n.toNearest(10, "floor"); // 10
48+
* n.toNearest(10, "ceil"); // 20
49+
* n.toNearest(0.25); // 17.25
50+
* ```
51+
*/
52+
toNearest(interval?: number, method?: 'round' | 'ceil' | 'floor'): number;
53+
54+
/**
55+
* A faster numeric between check which avoids type coercion to the Number object.
56+
* Since this avoids coercion, if non-numbers are passed in unpredictable results will occur. Use with caution.
57+
* @param a - The lower-bound
58+
* @param b - The upper-bound
59+
* @param inclusive - Include the bounding values as a true result?
60+
* @returns Is the number between the two bounds?
61+
*/
62+
between(a: number, b: number, inclusive?: boolean): boolean;
63+
}
64+
65+
interface NumberConstructor {
66+
/**
67+
* @see {@link Number#between}
68+
*/
69+
between(num: number, a: number, b: number, inclusive?: boolean): boolean;
70+
71+
/**
72+
* Test whether a value is numeric
73+
* This is the highest performing algorithm currently available, per https://jsperf.com/isnan-vs-typeof/5
74+
* @param n - A value to test
75+
* @returns Is it a number?
76+
*/
77+
isNumeric(n: unknown): boolean;
78+
79+
/**
80+
* Attempt to create a number from a user-provided string.
81+
* @param n - The value to convert; typically a string, but may already be a number.
82+
* @returns The number that the string represents, or NaN if no number could be determined.
83+
*/
84+
fromString(str: string | number): number;
85+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface RegExpConstructor {
2+
/**
3+
* Escape a given input string, prefacing special characters with backslashes for use in a regular expression
4+
* @param string - The un-escaped input string
5+
* @returns The escaped string, suitable for use in regular expression
6+
*/
7+
escape(string: string): string;
8+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
interface Set<T> {
2+
/**
3+
* Return the difference of two sets.
4+
* @param other - Some other set to compare against
5+
* @returns The difference defined as objects in this which are not present in other
6+
*/
7+
difference(other: Set<T>): Set<T>;
8+
9+
/**
10+
* Test whether this set is equal to some other set.
11+
* Sets are equal if they share the same members, independent of order
12+
* @param other - Some other set to compare against
13+
* @returns Are the sets equal?
14+
*/
15+
equals(other: Set<T>): boolean;
16+
17+
/**
18+
* Return the first value from the set.
19+
* @returns The first element in the set, or undefined
20+
*/
21+
first(): T | undefined;
22+
23+
/**
24+
* Return the intersection of two sets.
25+
* @param other - Some other set to compare against
26+
* @returns The intersection of both sets
27+
*/
28+
intersection(other: Set<T>): Set<T>;
29+
30+
/**
31+
* Test whether this set has an intersection with another set.
32+
* @param other - Another set to compare against
33+
* @returns Do the sets intersect?
34+
*/
35+
intersects(other: Set<T>): boolean;
36+
37+
/**
38+
* Test whether this set is a subset of some other set.
39+
* A set is a subset if all its members are also present in the other set.
40+
* @param other - Some other set that may be a subset of this one
41+
* @returns Is the other set a subset of this one?
42+
*/
43+
isSubset(other: Set<T>): boolean;
44+
45+
/**
46+
* Convert a set to a JSON object by mapping its contents to an array
47+
* @returns The set elements as an array.
48+
*/
49+
toObject(): T[];
50+
51+
/**
52+
* Test whether every element in this Set satisfies a certain test criterion.
53+
* @see {@link Array#every}
54+
* @param test - The test criterion to apply. Positional arguments are the value, the index of iteration, and the set being tested.
55+
* @returns Does every element in the set satisfy the test criterion?
56+
*/
57+
every(test: (value: T, index: number, set: Set<T>) => boolean): boolean;
58+
59+
/**
60+
* Filter this set to create a subset of elements which satisfy a certain test criterion.
61+
* @see {@link Array#filter}
62+
* @param test - The test criterion to apply. Positional arguments are the value, the index of iteration, and the set being filtered.
63+
* @returns A new Set containing only elements which satisfy the test criterion.
64+
*/
65+
filter<F extends T>(test: (value: T, index: number, set: Set<T>) => value is F): Set<F>;
66+
filter(test: (value: T, index: number, set: Set<T>) => boolean): Set<T>;
67+
68+
/**
69+
* Find the first element in this set which satisfies a certain test criterion.
70+
* @see {@link Array#find}
71+
* @param test - The test criterion to apply. Positional arguments are the value, the index of iteration, and the set being searched.
72+
* @returns The first element in the set which satisfies the test criterion, or undefined.
73+
*/
74+
find<F extends T>(test: (value: T, index: number, set: Set<T>) => value is F): F | undefined;
75+
find(test: (value: T, index: number, set: Set<T>) => boolean): T | undefined;
76+
77+
/**
78+
* Create a new Set where every element is modified by a provided transformation function.
79+
* @see {@link Array#map}
80+
* @param transform - The transformation function to apply.Positional arguments are the value, the index of iteration, and the set being transformed.
81+
* @returns A new Set of equal size containing transformed elements.
82+
*/
83+
map<V>(transform: (value: T, index: number, set: Set<T>) => V): Set<V>;
84+
85+
/**
86+
* Create a new Set with elements that are filtered and transformed by a provided reducer function.
87+
* @see {@link Array#reduce}
88+
* @param reducer - A reducer function applied to each value. Positional arguments are the accumulator, the value, the index of iteration, and the set being reduced.
89+
* @param accumulator - The initial value of the returned accumulator.
90+
* @returns The final value of the accumulator.
91+
*/
92+
reduce<V>(reducer: (accumulator: V, value: T, index: number, set: Set<T>) => V, accumulator: V): V;
93+
94+
/**
95+
* Test whether any element in this Set satisfies a certain test criterion.
96+
* @see {@link Array#some}
97+
* @param test - The test criterion to apply. Positional arguments are the value, the index of iteration, and the set being tested.
98+
* @returns Does any element in the set satisfy the test criterion?
99+
*/
100+
some(test: (value: T, index: number, set: Set<T>) => boolean): boolean;
101+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
interface String {
2+
/**
3+
* Capitalize a string, transforming it's first character to a capital letter
4+
*/
5+
capitalize<S extends string>(this: S): Capitalize<S>;
6+
7+
/**
8+
* Convert a string to Title Case where the first letter of each word is capitalized
9+
*/
10+
titleCase<S extends string>(this: S): Titlecase<S>;
11+
12+
/**
13+
* Strip any <script> tags which were included within a provided string
14+
*/
15+
stripScripts(): string;
16+
17+
/**
18+
* Transform any string into a url-viable slug string
19+
* @param options - Optional arguments which customize how the slugify operation is performed
20+
* @returns The slugified input string
21+
*/
22+
slugify(options?: String.SlugifyOptions): string;
23+
}
24+
25+
declare namespace String {
26+
interface SlugifyOptions {
27+
/**
28+
* The replacement character to separate terms
29+
* @defaultValue `'-'`
30+
*/
31+
replacement?: string;
32+
33+
/**
34+
* Replace all non-alphanumeric characters, or allow them?
35+
* @defaultValue `false`
36+
*/
37+
strict?: boolean;
38+
}
39+
}

0 commit comments

Comments
 (0)