1 | # `@geeebe/common`
|
2 |
|
3 | This 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 |
|
7 | In empty project directory.
|
8 |
|
9 | ```bash
|
10 | npm init
|
11 | # fill in details asked (defaults will do)
|
12 | npm i @geeebe/common
|
13 | npx geeebe init
|
14 | ```
|
15 |
|
16 | This 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 |
|
24 | To get started, write some `typescript` code in `src/index.ts` and run it.
|
25 |
|
26 | To 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 |
|
32 | Also 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 |
|
39 | Not too much here...
|
40 |
|
41 | HTTP response status
|
42 |
|
43 | ```typescript
|
44 | import { Statuses } from '@geeebe/common';
|
45 |
|
46 | // ...
|
47 |
|
48 | if (status === Statuses.NOT_FOUND) {} // 404
|
49 | ```
|
50 |
|
51 | Some simple functions for time and duration
|
52 |
|
53 | ```typescript
|
54 | import { Time } from '@geeebe/common';
|
55 |
|
56 | const fiveMinutes = Time.minutes(5); // 5 min in ms = 5 * 60 * 1000
|
57 | const sevenHours = Time.hours(7); // 7 hours in ms
|
58 | const someTime = Time.days(2) + Time.hours(3) + Time.seconds(2);
|
59 |
|
60 | // and then
|
61 | const hours = Time.toHours(sevenHours);
|
62 | // etc
|
63 |
|
64 | // also
|
65 | const fourHoursAgo = Time.past(Time.hours(4)); // Date() of 4 hours ago
|
66 | const future = Time.future(Time.seconds(45)); // or in the future
|
67 | ```
|
68 |
|
69 | Methods:
|
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 |
|
84 | Constants:
|
85 |
|
86 | * `SECOND = 1000`
|
87 | * `MINUTE = 60 * Time.SECOND`
|
88 | * `HOUR = 60 * Time.MINUTE`
|
89 | * `DAY = 24 * Time.HOUR`
|
90 |
|
91 | Async sleep function
|
92 |
|
93 | ```typescript
|
94 | await sleep(Time.seconds(45));
|
95 | ```
|