Skip to content

Commit

Permalink
deno (#35)
Browse files Browse the repository at this point in the history
* [WIP] deno

* [WIP] deno test; add sha256 for #27, for #30

* style: const range

* style: color-hash.ts -> mod.ts

* refactor: deno test for class ColorHash

* feature: use sha256 as default hash function, fixes #27, fixes #30

* chore: Makefile

* chore: package-lock.json

* chore: make build

* chore: update ci

* chore(workflows): setup deno with version 1.x

* chore: update ci

* chore: update ci

* docs: update usage
  • Loading branch information
zenozeng authored Apr 20, 2021
1 parent 3a094ac commit 2e602ef
Show file tree
Hide file tree
Showing 36 changed files with 372 additions and 4,295 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ name: "CodeQL"

on:
push:
branches: [ master ]
branches:
- '*'
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
branches:
- '*'
schedule:
- cron: '45 18 * * 1'

Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ jobs:
uses: actions/checkout@v2

- name: Setup Deno
uses: denolib/setup-deno@c7d7968ad4a59c159a777f79adddad6872ee8d96
uses: denoland/setup-deno@main
with:
deno-version: ${{ matrix.deno }} # tests across multiple Deno versions

- name: Cache Dependencies
run: deno cache deps.ts
deno-version: v1.x

- name: Run Tests
run: deno test -A --unstable
run: deno test
12 changes: 9 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches:
- '*'
pull_request:
branches: [ master ]
branches:
- '*'

jobs:
build:
Expand All @@ -25,6 +27,10 @@ jobs:
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Setup Deno
uses: denoland/setup-deno@main
with:
deno-version: v1.x
- run: npm ci
- run: npm run build --if-present
- run: make build
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Zeno Zeng
Copyright (c) 2015-2021 Zeno Zeng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test:
deno test

build:
mkdir -p dist
deno bundle mod.ts > dist/bundle.js
npx tsc --allowJs dist/bundle.js --outDir tmp
cp tmp/bundle.js dist/color-hash.js
rm -rf tmp
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@ https://zenozeng.github.io/color-hash/demo/

## Usage

### Browser
### Node.js

```bash
npm install color-hash
```

A UMD version of ColorHash is located in `dist/`.
```typescript
import ColorHash from 'color-hash'
```

or

Note that `Array.prototype.map` is used in `color-hash`,
a polyfill must be provided if you want to use it in IE8.
```javascript
const ColorHash = require('color-hash').default;
```

### <script type="module">

```html
<script type="module">
import ColorHash from '../dist/bundle.js';
</script>
```

#### Basic

Expand Down Expand Up @@ -84,16 +97,6 @@ var colorHash = new ColorHash({saturation: 0.5});
var colorHash = new ColorHash({saturation: [0.35, 0.5, 0.65]});
```

### Node.js

```bash
npm install color-hash --save
```

```javascript
var ColorHash = require('color-hash');
```

## License

MIT.
Expand Down Expand Up @@ -123,8 +126,7 @@ Simply sets lightness and saturation and change hue uniformly can generate unifo
### Test

```bash
npm install
npm test
deno test
```

#### Coverage Report
Expand Down
77 changes: 77 additions & 0 deletions color-hash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {assertEquals, assert} from 'https://deno.land/[email protected]/testing/asserts.ts';
import {generate as generateUUID} from 'https://deno.land/[email protected]/uuid/v4.ts';
import {HSL2RGB, RGB2HEX} from './lib/colors.ts';
import ColorHash from './mod.ts';

Deno.test("ColorHash#Hue: should return the hash color based on default hue", () => {
const hash = new ColorHash();
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(hue >= 0 && hue < 359, true); // hash % 359 means max 358
}
})

Deno.test("ColorHash#Hue: should return the hash color based on given hue value", () => {
const hash = new ColorHash({hue: 10});
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(hue, 10);
}
})

Deno.test("ColorHash#Hue: should return the hash color based on given hue range", () => {
for (let min = 0; min < 361; min += 60) {
for (let max = min + 1; max < 361; max += 60) {
const hash = new ColorHash({hue: {min, max}});
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(hue >= min && hue < max, true);
}
}
}
})

Deno.test("ColorHash#Hue: should work for multiple hue ranges", () => {
var ranges = [
{min: 30, max: 90},
{min: 180, max: 210},
{min: 270, max: 285}
];
const hash = new ColorHash({hue: ranges});
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(ranges.some((range) => hue >= range.min && hue < range.max), true);
}
})

Deno.test("ColorHash#LS: should return color based on given lightness and saturation", () => {
const hash = new ColorHash({lightness: 0.5, saturation: 0.5});
const [h, s, l] = hash.hsl(generateUUID());
assertEquals(s, 0.5);
assertEquals(l, 0.5);
})

Deno.test("ColorHash should return the hash color based on given lightness array and saturation array", () => {
const hash = new ColorHash({
lightness: [0.9, 1],
saturation: [0.9, 1]
})
const [h, s, l] = hash.hsl(generateUUID());
assertEquals([0.9, 1].includes(s), true);
assertEquals([0.9, 1].includes(l), true);
})

Deno.test("Custom hash function", () => {
const customHash = function (str: string) {
var hash = 0;
for (let i = 0; i < str.length; i++) {
hash += str.charCodeAt(i);
}
return hash;
};

const hash = new ColorHash({hash: customHash});
const h = customHash('abc') % 359;

assertEquals(hash.hsl('abc')[0], h);
})
1 change: 0 additions & 1 deletion coverage/coverage.json

This file was deleted.

Loading

0 comments on commit 2e602ef

Please sign in to comment.