add logging function
This commit is contained in:
parent
6694c7eb9a
commit
95ac45b5aa
2 changed files with 25 additions and 0 deletions
|
@ -3,3 +3,12 @@
|
|||
A collection of utility functions and classes extending base JavaScript.
|
||||
|
||||
Intended for the Deno runtime.
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { log_from, range } from "https://git.barnulf.net/mb/barnulf_ts/raw/branch/master/mod.ts"
|
||||
const log = log_from(import.meta)
|
||||
|
||||
for (const item of range(0, 10)) log(item)
|
||||
```
|
||||
|
|
|
@ -21,3 +21,19 @@ export function next<T>(iterator: Iterator<T>) {
|
|||
export function* range(from: number, to: number) {
|
||||
while (from < to) yield from++;
|
||||
}
|
||||
|
||||
/**
|
||||
* usage:
|
||||
* ```ts
|
||||
* const log = log_from(import.meta)
|
||||
*
|
||||
* const a = 4
|
||||
* log("a", a)
|
||||
* // [module.ts] a 4
|
||||
* ```
|
||||
*/
|
||||
export function log_from(meta: ImportMeta, logging_function = console.log) {
|
||||
const path = new URL(meta.url).pathname;
|
||||
const prefix = `[${path}]`;
|
||||
return (...args: unknown[]) => logging_function(prefix, ...args);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue