HyURL Utilities
Utility functions of HyURL collection.
This package currently contains the following functions, more functions may be
included in the future. Each function is stored in a separated file.
Since 0.1.50, this package now supports Node.js, Web and
Deno.
Import
There are two ways to import these functions.
All At Once
This method will load all functions into memory, even if you don't need some of
them.
import * as utils from "@hyurl/utils";
import { count, ensureType } from "@hyurl/utils";
Only Needed
This method will only load needed functions, which is recommended.
import count from "@hyurl/utils/count";
import ensureType from "@hyurl/utils/ensureType";
Utilities Types
Other than utility functions, this package also provides some utility types for
TypeScript, they are located in the following file and exposed to the global
namespace.
import "@hyurl/utils/types";
Web Support
When using this package in the browser, either loads it as a ESModule/CommonJS
Module as in Node.js, or loads the bundle file
./bundle/index.js instead.
<script src="./node_modules/@hyurl/utils/bundle/index.js"></script>
<script>
const { count } = window["@hyurl/utils"];
console.log(count([1,2,3]));
<script>
Deno Support
Yes, this package can be used directly in Deno, to use it,
just add this repository as a submodule to your project, and then import it
locally,
git submodule add https://github.com/hyurl/utils vendors/hyurl/utils
(It is recommended that you should store all your third party packages in a
common directory, for example, vendors
as used here.)
import * as utils from "./vendors/hyurl/utils/mod.ts";
However, there is a trick under the hood that allows a Node.js module working
in Deno, and it requires read permission of the disk for loading the Node.js
module.
For example, a file named test-deno.ts
:
import { sleep, timestamp } from "./vendors/hyurl/utils/mod.ts";
await sleep(1000);
console.log(timestamp());
You will need to use this command to run the program:
deno run --unstable --allow-read --allow-env test-deno.ts
Unit Test
In Node.js
npm test
In Deno
npm run test-deno
deno test --unstable --allow-read --allow-env test/deno/example.ts
Test as Submodule
deno test --unstable --allow-read --allow-env vendors/hyurl/utils/test/deno/example.ts