Skip to content

Commit

Permalink
Merge branch 'CodingCarter-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac committed Sep 18, 2020
2 parents 34c3f18 + a6aa287 commit b758ff9
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 61 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Expand Up @@ -15,13 +15,13 @@ jobs:
- name: Setup Deno
uses: denolib/setup-deno@master
with:
deno-version: 1.2
deno-version: 1.4

- name: Format
run: deno fmt --check
# - name: Format
# run: deno fmt --unstable --ignore=./bundle --check

- name: Lint
run: deno lint --unstable
# - name: Lint
# run: deno lint --unstable --ignore=./bundle

- name: Tests
run: deno test --unstable
run: deno test --unstable --coverage
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,13 @@
# Changelog

## 0.2.0 - [2020-09-17]

- new `Vendor` type
- new `isVendor()` for checking if something is a vendor
- added `./bundle/web/deno-vendors.js` bundle for the web
- updated `mod.ts`
- updated tests

## 0.1.0 - [2020-08-07]

- first release
84 changes: 66 additions & 18 deletions README.md
Expand Up @@ -17,31 +17,79 @@ not valid.
## Usage

```ts
import vendors from "https://deno.land/x/vendors/mod.ts";
import { vendors } from "https://deno.land/x/vendors/mod.ts";

console.log(vendors);
```

Output:

```sh
> [
> "ah",
> "apple",
> "atsc",
> "epub",
> "fx",
> "hp",
> "khtml",
> "moz",
> "ms",
> "o",
> "rim",
> "ro",
> "tc",
> "wap",
> "webkit",
> "xv",
> ]
```

You can also check if something is a correct `Vendor` by using `isVendor()`.

```ts
import { isVendor } from "https://deno.land/x/vendors/mod.ts";

console.log(isVendor("ms"));
console.log(isVendor("blah"));
```

Output:

```sh
> true
> false
```

Instead of running `isVendor()` if you need to assign a variable to a supposed `string` that is a vendor, you can just set the type of the variable to `Vendor`. Thankfully, the TypeScript compiler will make sure that your variable is one of the valid `vendors`.

```ts
[
'ah',
'apple',
'atsc',
'epub',
'hp',
'khtml',
'moz',
'ms',
'o',
'rim',
'ro',
'tc',
'wap',
'webkit',
'xv'
]
import { Vendor } from "https://deno.land/x/vendors/mod.ts";

const myVendor: Vendor = "o";
```

No output because it's valid!

Here is an example of what would happen if our variable isn't a valid `Vendor`:

```ts
import { Vendor } from "https://deno.land/x/vendors/mod.ts";

const myBadVendor1: Vendor = "";
const myBadVendor2: Vendor = 24;
```

Here are the two errors in the output

```sh
> error: TS2322 [ERROR]: Type '""' is not assignable to type '"ah" | "apple" | "atsc" | "epub" | "fx" | "hp" | "khtml" | "moz" | "ms" | "o" | "rim" | "ro" | "tc" | "wap" | "webkit" | "xv"'.
> const myBadVendor1: Vendor = "";
> ~~~~~~~~~~~~
>
> TS2322 [ERROR]: Type '24' is not assignable to type '"ah" | "apple" | "atsc" | "epub" | "fx" | "hp" | "khtml" | "moz" | "ms" | "o" | "rim" | "ro" | "tc" | "wap" | "webkit" | "xv"'.
> const myBadVendor2: Vendor = 24;
> ~~~~~~~~~~~~
```

### License
Expand Down
180 changes: 180 additions & 0 deletions bundle/web/deno-vendors.js
@@ -0,0 +1,180 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

// This is a specialised implementation of a System module loader.

"use strict";

// @ts-nocheck
/* eslint-disable */
let System, __instantiate;
(() => {
const r = new Map();

System = {
register(id, d, f) {
r.set(id, { d, f, exp: {} });
},
};
async function dI(mid, src) {
let id = mid.replace(/\.\w+$/i, "");
if (id.includes("./")) {
const [o, ...ia] = id.split("/").reverse(),
[, ...sa] = src.split("/").reverse(),
oa = [o];
let s = 0,
i;
while ((i = ia.shift())) {
if (i === "..") s++;
else if (i === ".") break;
else oa.push(i);
}
if (s < sa.length) oa.push(...sa.slice(s));
id = oa.reverse().join("/");
}
return r.has(id) ? gExpA(id) : import(mid);
}

function gC(id, main) {
return {
id,
import: (m) => dI(m, id),
meta: { url: id, main },
};
}

function gE(exp) {
return (id, v) => {
v = typeof id === "string" ? { [id]: v } : id;
for (const [id, value] of Object.entries(v)) {
Object.defineProperty(exp, id, {
value,
writable: true,
enumerable: true,
});
}
};
}

function rF(main) {
for (const [id, m] of r.entries()) {
const { f, exp } = m;
const { execute: e, setters: s } = f(gE(exp), gC(id, id === main));
delete m.f;
m.e = e;
m.s = s;
}
}

async function gExpA(id) {
if (!r.has(id)) return;
const m = r.get(id);
if (m.s) {
const { d, e, s } = m;
delete m.s;
delete m.e;
for (let i = 0; i < s.length; i++) s[i](await gExpA(d[i]));
const r = e();
if (r) await r;
}
return m.exp;
}

function gExp(id) {
if (!r.has(id)) return;
const m = r.get(id);
if (m.s) {
const { d, e, s } = m;
delete m.s;
delete m.e;
for (let i = 0; i < s.length; i++) s[i](gExp(d[i]));
e();
}
return m.exp;
}
__instantiate = (m, a) => {
System = __instantiate = undefined;
rF(m);
return a ? gExpA(m) : gExp(m);
};
})();

System.register("vendors", [], function (exports_1, context_1) {
"use strict";
var vendors;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
execute: function () {
exports_1("vendors", vendors = [
"ah",
"apple",
"atsc",
"epub",
"fx",
"hp",
"khtml",
"moz",
"ms",
"o",
"rim",
"ro",
"tc",
"wap",
"webkit",
"xv",
]);
}
};
});
System.register("types/vendor", [], function (exports_2, context_2) {
"use strict";
var __moduleName = context_2 && context_2.id;
return {
setters: [],
execute: function () {
}
};
});
System.register("is_vendor", ["vendors"], function (exports_3, context_3) {
"use strict";
var vendors_ts_1;
var __moduleName = context_3 && context_3.id;
function isVendor(vendor) {
return vendors_ts_1.vendors.includes(vendor);
}
exports_3("isVendor", isVendor);
return {
setters: [
function (vendors_ts_1_1) {
vendors_ts_1 = vendors_ts_1_1;
}
],
execute: function () {
}
};
});
System.register("mod", ["vendors", "is_vendor"], function (exports_4, context_4) {
"use strict";
var __moduleName = context_4 && context_4.id;
return {
setters: [
function (vendors_ts_2_1) {
exports_4({
"vendors": vendors_ts_2_1["vendors"]
});
},
function (is_vendor_ts_1_1) {
exports_4({
"isVendor": is_vendor_ts_1_1["isVendor"]
});
}
],
execute: function () {
}
};
});

const __exp = __instantiate("mod", false);
export const Vendor = __exp["Vendor"];
export const vendors = __exp["vendors"];
export const isVendor = __exp["isVendor"];
12 changes: 12 additions & 0 deletions is_vendor.ts
@@ -0,0 +1,12 @@
import type { Vendor } from "./types/vendor.ts";
import { vendors } from "./vendors.ts";

/**
* Returns whether a given `vendor` string is an official vendor.
*
* @param vendor the given vendor string
* @returns boolean for if the `vendor` argument is in the `vendors` list
*/
export function isVendor(vendor: string) {
return vendors.includes(<Vendor> vendor);
}
22 changes: 3 additions & 19 deletions mod.ts
@@ -1,19 +1,3 @@
const venders = [
"ah",
"apple",
"atsc",
"epub",
"hp",
"khtml",
"moz",
"ms",
"o",
"rim",
"ro",
"tc",
"wap",
"webkit",
"xv",
] as const;

export default venders;
export type { Vendor } from "./types/vendor.ts";
export { vendors } from "./vendors.ts";
export { isVendor } from "./is_vendor.ts";
18 changes: 0 additions & 18 deletions mod_test.ts

This file was deleted.

4 changes: 4 additions & 0 deletions tests/deps.ts
@@ -0,0 +1,4 @@
export {
assert,
assertStrictEquals,
} from "https://deno.land/std@0.69.0/testing/asserts.ts";
17 changes: 17 additions & 0 deletions tests/is_vendor.test.ts
@@ -0,0 +1,17 @@
import { assert } from "./deps.ts";

import { isVendor, vendors } from "../mod.ts";

Deno.test("isVendor()", () => {
for (const vendor of vendors) {
assert(isVendor(vendor), `\`isVendor("${vendor}")\` should return true`);
}

const falseVendors = ["", "not a vendor", "def not a vendor"];
for (const falseVendor of falseVendors) {
assert(
!isVendor(falseVendor),
`\`isVendor("${falseVendor}")\` should return false`,
);
}
});

0 comments on commit b758ff9

Please sign in to comment.