Skip to content

Commit 71fc31c

Browse files
committed
Convert a few small files to TS
1 parent d4425fc commit 71fc31c

File tree

7 files changed

+52
-58
lines changed

7 files changed

+52
-58
lines changed

src/lib/clean_number.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/lib/clean_number.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
import isNumeric from 'fast-isnumeric';
4+
import { BADNUM } from '../constants/numerical';
5+
6+
// precompile for speed
7+
const JUNK = /^['"%,$#\s']+|[, ]|['"%,$#\s']+$/g;
8+
9+
/**
10+
* cleanNumber: remove common leading and trailing cruft
11+
* Always returns either a number or BADNUM.
12+
*/
13+
function cleanNumber(v: any): number | undefined {
14+
if (typeof v === 'string') v = v.replace(JUNK, '');
15+
if (isNumeric(v)) return Number(v);
16+
17+
return BADNUM;
18+
}
19+
20+
export default cleanNumber;

src/lib/mod.js renamed to src/lib/mod.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,15 @@
44
* sanitized modulus function that always returns in the range [0, d)
55
* rather than (-d, 0] if v is negative
66
*/
7-
function mod(v, d) {
8-
var out = v % d;
7+
export function mod(v: number, d: number) {
8+
const out = v % d;
99
return out < 0 ? out + d : out;
1010
}
1111

1212
/**
1313
* sanitized modulus function that always returns in the range [-d/2, d/2]
1414
* rather than (-d, 0] if v is negative
1515
*/
16-
function modHalf(v, d) {
17-
return Math.abs(v) > (d / 2) ?
18-
v - Math.round(v / d) * d :
19-
v;
16+
export function modHalf(v: number, d: number) {
17+
return Math.abs(v) > d / 2 ? v - Math.round(v / d) * d : v;
2018
}
21-
22-
module.exports = {
23-
mod: mod,
24-
modHalf: modHalf
25-
};

src/lib/regex.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/lib/regex.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const NUMBER_REGEX = '([2-9]|[1-9][0-9]+)?';
4+
5+
/**
6+
* make a regex for matching counter ids/names ie xaxis, xaxis2, xaxis10...
7+
*
8+
* @param head: the head of the pattern, eg 'x' matches 'x', 'x2', 'x10' etc.
9+
* 'xy' is a special case for cartesian subplots: it matches 'x2y3' etc
10+
* @param tail: a fixed piece after the id
11+
* eg counterRegex('scene', '.annotations') for scene2.annotations etc.
12+
* @param openEnded: if true, the string may continue past the match.
13+
* @param matchBeginning: if false, the string may start before the match.
14+
*/
15+
export function counter(head: string, tail: string = '', openEnded: boolean, matchBeginning: boolean) {
16+
const fullTail = tail + (openEnded ? '' : '$');
17+
const startWithPrefix = matchBeginning === false ? '' : '^';
18+
return head === 'xy'
19+
? new RegExp(startWithPrefix + 'x' + NUMBER_REGEX + 'y' + NUMBER_REGEX + fullTail)
20+
: new RegExp(startWithPrefix + head + NUMBER_REGEX + fullTail);
21+
}

src/lib/sort_object_keys.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/lib/sort_object_keys.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
function sortObjectKeys(obj: Record<string, any>) {
4+
return Object.keys(obj).sort();
5+
}
6+
7+
export default sortObjectKeys;

0 commit comments

Comments
 (0)