UNPKG

2.28 kBMarkdownView Raw
1# `@geeebe/common`
2
3This library has tiny bit of useful code, plus a util for setting up a `typescript` project with some common useful settings including `ts-node`, `tslint` and `jest`.
4
5## Getting Started
6
7In empty project directory.
8
9```bash
10npm init
11# fill in details asked (defaults will do)
12npm i @geeebe/common
13npx geeebe init
14```
15
16This will do the following for your project
17
18* create `.editorconfig`, `.gitignore` and `.npmignore`
19* install `typescript` and `ts-node`
20* install and configure `tslint`
21* install `jest` with `ts-jest` and create a starter config
22* install and configure `nodemon`
23
24To get started, write some `typescript` code in `src/index.ts` and run it.
25
26To run, either use:
27
28* `npm start` to run one-off using `ts-node`
29* `npm run watch` to run and watch for changes using `nodemon` and `ts-node`
30* `npm run build` to build output to `dist/` using `tsc`
31
32Also useful:
33
34* `npm test` to run `jest` tests in the `test/` directory (looking for `*.test.ts` files)
35* `npm run lint` to run `tslint`
36
37## Included Code
38
39Not too much here...
40
41HTTP response status
42
43```typescript
44import { Statuses } from '@geeebe/common';
45
46// ...
47
48if (status === Statuses.NOT_FOUND) {} // 404
49```
50
51Some simple functions for time and duration
52
53```typescript
54import { Time } from '@geeebe/common';
55
56const fiveMinutes = Time.minutes(5); // 5 min in ms = 5 * 60 * 1000
57const sevenHours = Time.hours(7); // 7 hours in ms
58const someTime = Time.days(2) + Time.hours(3) + Time.seconds(2);
59
60// and then
61const hours = Time.toHours(sevenHours);
62// etc
63
64// also
65const fourHoursAgo = Time.past(Time.hours(4)); // Date() of 4 hours ago
66const future = Time.future(Time.seconds(45)); // or in the future
67```
68
69Methods:
70
71* `ms(ms: number): Duration`
72* `seconds(s: number): Duration`
73* `minutes(m: number): Duration`
74* `hours(h: number): Duration`
75* `days(d: number): Duration`
76* `toMs(d: Duration): number`
77* `toSeconds(d: Duration): number`
78* `toMinutes(d: Duration): number`
79* `toHours(d: Duration): number`
80* `toDays(d: Duration): number`
81* `past(interval: Duration): Date`
82* `future(interval: Duration): Date`
83
84Constants:
85
86* `SECOND = 1000`
87* `MINUTE = 60 * Time.SECOND`
88* `HOUR = 60 * Time.MINUTE`
89* `DAY = 24 * Time.HOUR`
90
91Async sleep function
92
93```typescript
94await sleep(Time.seconds(45));
95```