Skip to content

Commit 47b52d6

Browse files
committedJul 30, 2022
feat: first blood, should just work
1 parent 267e372 commit 47b52d6

16 files changed

+257
-35
lines changed
 

‎.changeset/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"changelog": [
44
"@changesets/changelog-github",
55
{
6-
"repo": "un-ts/lib-boilerplate"
6+
"repo": "un-ts/ab64"
77
}
88
],
99
"commit": false,

‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ dist
33
lib
44
CHANGELOG.md
55
/pnpm-lock.yaml
6+
!/.github
67
!/.*.cjs

‎.eslintrc

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
"root": true,
33
"extends": "@1stg",
44
"overrides": [
5+
{
6+
"files": ".github/*.yml",
7+
"rules": {
8+
"unicorn/filename-case": "off"
9+
}
10+
},
511
{
612
"files": "docs/**/*.tsx",
713
"rules": {

‎.github/workflows/pages.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Deploy GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout Repo
13+
uses: actions/checkout@v3
14+
15+
- name: Setup pnpm
16+
uses: pnpm/action-setup@v2
17+
with:
18+
version: latest
19+
20+
- name: Setup Node.js 16
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: 16
24+
cache: pnpm
25+
26+
- name: Install Dependencies
27+
run: pnpm i
28+
29+
- name: Build Docs
30+
run: yarn docs:build
31+
32+
- name: Add misc files
33+
run: |
34+
touch dist/.nojekyll
35+
echo ab64.js.org > dist/CNAME
36+
37+
- name: Deploy 🚀
38+
uses: JamesIves/github-pages-deploy-action@v4
39+
with:
40+
folder: dist
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎.github/workflows/release.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
with:
3636
publish: pnpm release
3737
version: pnpm run version
38-
commit: 'chore: release lib-boilerplate'
39-
title: 'chore: release lib-boilerplate'
38+
commit: 'chore: release ab64'
39+
title: 'chore: release ab64'
4040
env:
4141
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4242
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

‎.github/workflows/vercel.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- main
7+
# eslint-disable-next-line yml/no-empty-mapping-value
78
pull_request_target:
89

910
jobs:

‎README.md

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# lib-boilerplate
1+
# ab64
22

3-
[![GitHub Actions](https://github.com/un-ts/lib-boilerplate/workflows/CI/badge.svg)](https://github.com/un-ts/lib-boilerplate/actions/workflows/ci.yml)
4-
[![Codecov](https://img.shields.io/codecov/c/github/un-ts/lib-boilerplate.svg)](https://codecov.io/gh/un-ts/lib-boilerplate)
5-
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/un-ts/lib-boilerplate.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/un-ts/lib-boilerplate/context:javascript)
6-
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fun-ts%2Flib-boilerplate%2Fmain%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
7-
[![npm](https://img.shields.io/npm/v/lib-boilerplate.svg)](https://www.npmjs.com/package/lib-boilerplate)
8-
[![GitHub Release](https://img.shields.io/github/release/un-ts/lib-boilerplate)](https://github.com/un-ts/lib-boilerplate/releases)
3+
[![GitHub Actions](https://github.com/un-ts/ab64/workflows/CI/badge.svg)](https://github.com/un-ts/ab64/actions/workflows/ci.yml)
4+
[![Codecov](https://img.shields.io/codecov/c/github/un-ts/ab64.svg)](https://codecov.io/gh/un-ts/ab64)
5+
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/un-ts/ab64.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/un-ts/ab64/context:javascript)
6+
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fun-ts%2Fab64%2Fmain%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
7+
[![npm](https://img.shields.io/npm/v/ab64.svg)](https://www.npmjs.com/package/ab64)
8+
[![GitHub Release](https://img.shields.io/github/release/un-ts/ab64)](https://github.com/un-ts/ab64/releases)
99

1010
[![Conventional Commits](https://img.shields.io/badge/conventional%20commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
1111
[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com)
1212
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
1313
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
1414
[![changesets](https://img.shields.io/badge/maintained%20with-changesets-176de3.svg)](https://github.com/changesets/changesets)
1515

16-
A simple library boilerplate.
16+
The smallest and fastest Base64 implementation in JavaScript
1717

1818
## TOC <!-- omit in toc -->
1919

@@ -31,21 +31,35 @@ A simple library boilerplate.
3131

3232
```sh
3333
# pnpm
34-
pnpm add lib-boilerplate
34+
pnpm add ab64
3535

3636
# yarn
37-
yarn add lib-boilerplate
37+
yarn add ab64
3838

3939
# npm
40-
npm i lib-boilerplate
40+
npm i ab64
4141
```
4242

4343
### API
4444

4545
```js
46-
import echo from 'lib-boilerplate'
46+
import { decode, decodeUrl, encode, encodeUrl } from 'ab64'
4747

48-
echo()
48+
encode('Hello World!') // SGVsbG8gV29ybGQh
49+
50+
encode('dankogai') // ZGFua29nYWk=
51+
encodeUrl('dankogai') // ZGFua29nYWk
52+
53+
encode('小飼弾') // 5bCP6aO85by+
54+
encodeUrl('小飼弾') // 5bCP6aO85by-
55+
56+
decode('SGVsbG8gV29ybGQh') // Hello World!
57+
58+
decode('ZGFua29nYWk=') // dankogai
59+
decodeUrl('ZGFua29nYWk') // dankogai
60+
61+
decode('5bCP6aO85by+') // 小飼弾
62+
decodeUrl('5bCP6aO85by-') // 小飼弾
4963
```
5064

5165
## Sponsors

‎docs/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "lib-boilerplate-docs",
2+
"name": "ab64-docs",
33
"type": "commonjs",
4-
"description": "A simple library boilerplate."
4+
"description": "The smallest and fastest Base64 implementation in JavaScript"
55
}

‎package.json

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "lib-boilerplate",
3-
"version": "0.0.2",
2+
"name": "ab64",
3+
"version": "0.0.0",
44
"type": "module",
5-
"description": "A simple library boilerplate.",
6-
"repository": "git+https://github.com/un-ts/lib-boilerplate.git",
5+
"description": "The smallest and fastest Base64 implementation in JavaScript",
6+
"repository": "git+https://github.com/un-ts/ab64.git",
77
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
88
"donate": {
99
"recipients": [
@@ -62,10 +62,11 @@
6262
"postversion": "pnpm i --no-frozen-lockfile",
6363
"prepare": "simple-git-hooks",
6464
"prerelease": "pnpm build",
65-
"prevercel-build": "pnpm build",
6665
"release": "changeset publish",
6766
"serve": "sirv dist -s",
68-
"test": "vitest run --coverage",
67+
"test": "run-p test:*",
68+
"test:browser": "vitest run --coverage",
69+
"test:node": "cross-env TEST_ENV=node vitest run --coverage",
6970
"typecov": "type-coverage",
7071
"vercel-build": "pnpm docs:build",
7172
"version": "changeset version"
@@ -87,8 +88,9 @@
8788
"@types/react-dom": "^18.0.6",
8889
"@types/web": "^0.0.70",
8990
"c8": "^7.12.0",
91+
"cross-env": "^7.0.3",
92+
"edge-runtime": "1.1.0-beta.23",
9093
"github-markdown-css": "^5.1.0",
91-
"lib-boilerplate": "link:.",
9294
"react": "^18.2.0",
9395
"react-dom": "^18.2.0",
9496
"react-router-dom": "^6.3.0",
@@ -104,14 +106,16 @@
104106
"module": "./lib/index.js",
105107
"exports": {
106108
"types": "./lib/index.d.ts",
109+
"browser": "./lib/browser.js",
107110
"import": "./lib/index.js",
108111
"require": "./lib/index.cjs"
109112
},
110113
"types": "./lib/index.d.ts"
111114
},
112115
"size-limit": [
113116
{
114-
"path": "lib/index.js"
117+
"path": "lib/browser.js",
118+
"limit": "280B"
115119
}
116120
],
117121
"typeCoverage": {

‎pnpm-lock.yaml

+75-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/browser.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const HEX = 16
2+
const CHUNK = 4
3+
4+
export const decode = (val: string) =>
5+
decodeURIComponent(
6+
[
7+
// eslint-disable-next-line sonar/deprecation -- it's fine on browser
8+
...atob(val),
9+
]
10+
.map(
11+
char => '%' + ('00' + char.codePointAt(0)!.toString(HEX)).slice(-1 * 2),
12+
)
13+
.join(''),
14+
)
15+
16+
export const encode = (val: string) =>
17+
// eslint-disable-next-line sonar/deprecation -- it's fine on browser
18+
btoa(
19+
encodeURIComponent(val).replace(/%([\dA-F]{2})/g, (_, $1: string) =>
20+
// eslint-disable-next-line unicorn/prefer-number-properties -- for smaller size
21+
String.fromCodePoint(parseInt($1, HEX)),
22+
),
23+
)
24+
25+
export const decodeUrl = (val: string) => {
26+
let output = val.replace(/-/g, '+').replace(/_/g, '/')
27+
switch (output.length % CHUNK) {
28+
case 0:
29+
break
30+
case 2:
31+
output += '=='
32+
break
33+
case 3:
34+
output += '='
35+
break
36+
default:
37+
break
38+
}
39+
return decode(output)
40+
}
41+
42+
export const encodeUrl = (val: string) =>
43+
encode(val).replace(/\+/g, '-').replace(/=/g, '').replace(/\//g, '_')

‎src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
export default () => 'Hello World!'
1+
export const decode = (val: string) => Buffer.from(val, 'base64').toString()
2+
3+
export const encode = (val: string) => Buffer.from(val).toString('base64')
4+
5+
export const decodeUrl = (val: string) =>
6+
Buffer.from(val, 'base64url').toString()
7+
8+
export const encodeUrl = (val: string) => Buffer.from(val).toString('base64url')

‎test/basic.spec.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
import echo from 'lib-boilerplate'
1+
import { decode, decodeUrl, encode, encodeUrl } from 'ab64'
22

3-
test('it should just work', () => {
4-
expect(echo()).toBe('Hello World!')
3+
describe('encoding', () => {
4+
test('it should just work', () => {
5+
expect(encode('Hello World!')).toBe('SGVsbG8gV29ybGQh')
6+
7+
expect(encode('dankogai')).toBe('ZGFua29nYWk=')
8+
expect(encodeUrl('dankogai')).toBe('ZGFua29nYWk')
9+
10+
expect(encode('小飼弾')).toBe('5bCP6aO85by+')
11+
expect(encodeUrl('小飼弾')).toBe('5bCP6aO85by-')
12+
})
13+
})
14+
15+
describe('decoding', () => {
16+
test('it should just work', () => {
17+
expect(decode('SGVsbG8gV29ybGQh')).toBe('Hello World!')
18+
19+
expect(decode('ZGFua29nYWk=')).toBe('dankogai')
20+
expect(decodeUrl('ZGFua29nYWk')).toBe('dankogai')
21+
22+
expect(decode('5bCP6aO85by+')).toBe('小飼弾')
23+
expect(decodeUrl('5bCP6aO85by-')).toBe('小飼弾')
24+
})
525
})

‎tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "@1stg/tsconfig/node16",
33
"compilerOptions": {
4-
"rootDir": "."
4+
"rootDir": ".",
5+
"removeComments": true
56
}
67
}

‎vercel.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": 2,
33
"alias": [
4-
"lib-boilerplate.vercel.app"
4+
"ab64.vercel.app"
55
],
66
"github": {
77
"silent": true

‎vitest.config.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
import path from 'node:path'
2+
13
import autoImport from 'unplugin-auto-import/vite'
24
import { defineConfig } from 'vitest/config'
35

6+
const isNode = process.env.TEST_ENV === 'node'
7+
48
export default defineConfig({
9+
resolve: {
10+
alias: {
11+
ab64: path.resolve(`src/${isNode ? 'index' : 'browser'}`),
12+
},
13+
},
514
plugins: [
6-
// TODO: report the TypeScript issue
15+
/** @see https://github.com/microsoft/TypeScript/issues/50067 */
716
(
817
autoImport as unknown as typeof import('unplugin-auto-import/vite')['default']
918
)({
@@ -14,5 +23,6 @@ export default defineConfig({
1423
coverage: {
1524
reporter: ['lcov', 'json', 'text'],
1625
},
26+
environment: isNode ? 'node' : 'edge-runtime',
1727
},
1828
})

0 commit comments

Comments
 (0)
Please sign in to comment.