Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on:
pull_request:
push:
branches: [master]

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
check:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version-file: package.json
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 24
- run: bun install --frozen-lockfile
- run: bun run check
- run: bun run build
- run: bun test
67 changes: 19 additions & 48 deletions .github/workflows/update-indexes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,33 @@ name: Update Indexes

on:
schedule:
# Run weekly on Sunday at 00:00 UTC
# Sundays at 00:00 UTC
- cron: '0 0 * * 0'
workflow_dispatch:
inputs:
version:
description: 'Index version (e.g., 1.0.1)'
required: true
description: 'Index version (default: YYYY.MM.DD)'
required: false

permissions:
contents: write

jobs:
build-and-release:
runs-on: ubuntu-latest

timeout-minutes: 120
steps:
- uses: actions/checkout@v4

- uses: oven-sh/setup-bun@v2
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
bun-version: latest

- name: Install dependencies
run: bun install

- name: Install all iconify packages
run: |
# Get list of all icon sets
PREFIXES=$(curl -s https://api.iconify.design/collections | jq -r 'keys[]')

# Install each package (skip if not available)
for prefix in $PREFIXES; do
bun add -d @iconify-json/$prefix 2>/dev/null || true
done

- name: Build all indexes
run: bun run scripts/build-all-indexes.ts

- name: Determine version
id: version
run: |
if [ -n "${{ github.event.inputs.version }}" ]; then
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
else
echo "version=$(date +%Y.%m.%d)" >> $GITHUB_OUTPUT
fi

- name: Create Release
persist-credentials: false
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version-file: package.json
- run: bun install --frozen-lockfile
- run: bun run install:icon-sets
- run: bun run build:all-indexes
- run: bun run release:indexes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ steps.version.outputs.version }}
run: |
TAG="index-v${VERSION}"

# Delete existing release if exists
gh release delete $TAG --yes 2>/dev/null || true

# Create release with all index files
gh release create $TAG \
--title "Index v${VERSION}" \
--notes "Automated index update $(date +%Y-%m-%d)" \
dist/indexes/*
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
VERSION: ${{ inputs.version }}
33 changes: 33 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "./node_modules/oxlint/configuration_schema.json",
"plugins": ["typescript", "import", "unicorn", "promise", "node"],
"env": { "node": true, "es2024": true },
"rules": {
"no-unused-vars": "error",
"eqeqeq": ["error", "always", { "null": "ignore" }],
"typescript/no-explicit-any": "error",
"typescript/no-non-null-assertion": "error",
"typescript/no-floating-promises": "error",
"typescript/no-misused-promises": "error",
"typescript/consistent-type-imports": "error",
"typescript/consistent-return": "error",
"typescript/no-unnecessary-condition": "error",
"typescript/no-unnecessary-type-assertion": "error",
"typescript/no-unnecessary-type-parameters": "error",
"typescript/no-unnecessary-boolean-literal-compare": "error",
"typescript/no-unnecessary-template-expression": "error",
"typescript/no-unnecessary-type-arguments": "error",
"typescript/prefer-optional-chain": "error",
"typescript/prefer-for-of": "error",
"import/no-cycle": "error",
"import/no-self-import": "error",
"import/no-duplicates": "error",
"import/no-mutable-exports": "error",
"unicorn/no-instanceof-array": "error",
"unicorn/no-typeof-undefined": "error",
"unicorn/no-nested-ternary": "error",
"unicorn/no-unnecessary-await": "error",
"unicorn/prefer-string-starts-ends-with": "error",
"unicorn/prefer-node-protocol": "error"
}
}
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ npx whaticon icon.svg --json
| `popular` | ~57k | ~1.8 MB | 12 popular sets (default) |
| `full` | ~200k+ | ~6 MB | All Iconify sets |

The `popular` index is bundled with the package. Other variants are downloaded on first use to `~/.cache/whaticon/`.
The `popular` index is bundled with the package. Other variants are downloaded on first use to `$XDG_CACHE_HOME/whaticon/` (`~/.cache/whaticon/` by default), and downloaded again once a whaticon update hashes icons another way.

## API Usage

Expand Down Expand Up @@ -101,15 +101,31 @@ Ensure index is downloaded, returns gzipped buffers.
#### `isIndexDownloaded(variant)`
Check if index variant is cached locally.

### Without Node

`whaticon/hash` has no Node or native dependencies, for hosts that rasterize SVG themselves, such as a browser canvas or an app's own renderer. Rasterize the icon as the index was built: drawn at the largest size that fits a 33 by 32 box, centered, on transparency, then one byte per pixel of `255 - alpha`, so the shapes' coverage counts and their color does not.

```ts
import { dHashFromPixels, findMatchesByHash, parseIndex } from 'whaticon/hash'

// names.txt and hashes.bin, ungzipped, for example with DecompressionStream
const index = parseIndex(namesText, hashesBytes)
const matches = findMatchesByHash(dHashFromPixels(grayscalePixels), index, { threshold: 0.9 })
```

Icons whose hashes are identical, as sets that copy another's are, come back as one match with the others in `siblings`, led by a `prefer`red set's icon. An index's `metadata.json` records the `scheme` it was hashed with; `isCompatibleIndex(metadata)` tells whether this version hashes the same way.

## Building Custom Index

```typescript
import { writeFileSync } from 'node:fs'
import { gzipSync } from 'node:zlib'
import { buildIndex } from 'whaticon'
import { gzipSync, writeFileSync } from 'fs'

// The body and viewBox of each icon, as @iconify/utils' iconToSVG returns them
const icons = [
{ name: 'my:icon1', svg: '<svg>...</svg>' },
{ name: 'my:icon2', svg: '<svg>...</svg>' },
{ name: 'my:icon1', body: '<path d="..."/>', viewBox: [0, 0, 24, 24] },
{ name: 'my:icon2', body: '<path d="..."/>', viewBox: [0, 0, 512, 448] },
]

const { names, hashes } = await buildIndex(icons)
Expand All @@ -120,14 +136,14 @@ writeFileSync('hashes.bin.gz', gzipSync(hashes))

## How It Works

1. **Render**: SVG is rendered to a 33×32 grayscale image using [sharp](https://sharp.pixelplumbing.com/)
1. **Render**: SVG is rendered with [sharp](https://sharp.pixelplumbing.com/) to a 33×32 image of how much its shapes cover each pixel, whatever their color, fitted and centered
2. **Hash**: A [difference hash (dHash)](https://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html) is computed — comparing adjacent pixels to produce 1024 bits
3. **Match**: Hamming distance between hashes determines similarity

### Performance

- **Index build**: ~10,000 icons/sec (sprite sheet batching)
- **Search**: ~5ms per query (32-bit popcount optimization)
- **Index build**: ~700 icons/sec, each hashed exactly as a query is
- **Search**: ~15ms per query over the popular index (32-bit popcount)
- **Index size**: ~30 bytes per icon (gzip compressed)

## Index Format
Expand Down
Loading
Loading