这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [22, 20, 18]
node-version: [24, 22, 20]

steps:
- name: Checkout repository
Expand All @@ -27,5 +27,8 @@ jobs:
- name: Install dependencies
run: yarn install

- name: Build package
run: yarn build

- name: Run tests
run: yarn test
4 changes: 4 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"spec": "test/**/*.ts",
"node-option": ["import=tsx"]
}
46 changes: 32 additions & 14 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { defineConfig } from "eslint/config";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
import js from '@eslint/js'
import { FlatCompat } from '@eslint/eslintrc'
import tsPlugin from '@typescript-eslint/eslint-plugin'
import tsParser from '@typescript-eslint/parser'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
})

export default defineConfig([{
extends: compat.extends("airbnb"),
}]);
export default [
js.configs.recommended,
...compat.extends('airbnb/base', 'prettier'),
{
files: ['src/**/*.ts', 'test/**/*.ts'],
languageOptions: {
parser: tsParser,
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
},
plugins: {
'@typescript-eslint': tsPlugin,
},
rules: {
...tsPlugin.configs.recommended.rules,
},
},
]
49 changes: 32 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
{
"name": "passes-wcag",
"version": "0.3.0",
"description": "Evaluate if a to colors have sufficient contrast to pass WCAG requirements.",
"description": "Evaluate if two colors have sufficient contrast to pass WCAG requirements.",
"type": "module",
"main": "build/index.js",
"types": "build/index.d.ts",
"exports": {
".": {
"types": "./build/index.d.ts",
"import": "./build/index.js"
}
},
"files": [
"build"
],
"scripts": {
"build": "babel src -d build",
"lint": "eslint --max-warnings=0 src",
"mocha": "mocha --require @babel/register",
"prepublish": "yarn build && yarn test",
"postpublish": "git push --tags",
"release": "np",
"test": "yarn lint && yarn mocha"
"build": "tsc",
"lint": "eslint --max-warnings=0 src/**/*.ts",
"mocha": "mocha",
"test": "yarn lint && yarn mocha",
"prepare": "yarn build",
"prepublishOnly": "yarn test",
"preversion": "yarn test",
"release": "np"
},
"repository": {
"type": "git",
Expand All @@ -35,19 +47,22 @@
},
"homepage": "https://github.com/misund/passes-wcag#readme",
"devDependencies": {
"@babel/cli": "7.28.0",
"@babel/core": "7.28.0",
"@babel/preset-env": "7.28.0",
"@babel/register": "7.27.1",
"@eslint/eslintrc": "3.3.1",
"@eslint/js": "9.32.0",
"@types/mocha": "10.0.10",
"@types/node": "22.10.5",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.18.0",
"eslint": "9.32.0",
"eslint-config-airbnb": "17.1.1",
"eslint-config-airbnb": "19.0.4",
"eslint-config-prettier": "10.1.8",
"eslint-plugin-import": "2.32.0",
"eslint-plugin-jsx-a11y": "6.10.2",
"eslint-plugin-react": "7.37.5",
"mocha": "11.7.1",
"np": "10.2.0"
"np": "10.2.0",
"tsx": "4.19.2",
"typescript": "5.9.2"
},
"dependencies": {
"get-contrast-ratio": "0.2.1"
"get-contrast-ratio": "1.0.0-1"
}
}
6 changes: 0 additions & 6 deletions src/index.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import getContrastRatio from 'get-contrast-ratio'

const passes = (color1: string, color2: string, threshold: number): boolean =>
getContrastRatio(color1, color2) >= threshold

export const passesWcagAaLargeText = (color1: string, color2: string): boolean =>
passes(color1, color2, 3)

export const passesWcagAa = (color1: string, color2: string): boolean =>
passes(color1, color2, 4.5)

export const passesWcagAaa = (color1: string, color2: string): boolean =>
passes(color1, color2, 7)
17 changes: 17 additions & 0 deletions test/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* global describe, it */
import assert from 'assert'
import { passesWcagAaLargeText, passesWcagAa, passesWcagAaa } from '../build/index.js'

describe('Built output', () => {
it('should export working ESM modules', () => {
assert.strictEqual(typeof passesWcagAaLargeText, 'function')
assert.strictEqual(typeof passesWcagAa, 'function')
assert.strictEqual(typeof passesWcagAaa, 'function')
})

it('should evaluate WCAG compliance correctly from built output', () => {
assert.strictEqual(passesWcagAaLargeText('#333', '#777'), false)
assert.strictEqual(passesWcagAa('black', 'darkgoldenrod'), true)
assert.strictEqual(passesWcagAaa('black', 'darkgoldenrod'), false)
})
})
44 changes: 20 additions & 24 deletions test/test.js → test/test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
/* global describe, it */
import assert from 'assert';
import assert from 'assert'

import {
passesWcagAaLargeText,
passesWcagAa,
passesWcagAaa,
} from '..';
import { passesWcagAaLargeText, passesWcagAa, passesWcagAaa } from '../src/index.js'

describe('passes-wcag', () => {
describe('passesWcagAaLargeText', () => {
it('#333 on #777 does not pass wcag aa for large text', () => {
assert.equal(false, passesWcagAaLargeText('#333', '#777'));
});
assert.equal(false, passesWcagAaLargeText('#333', '#777'))
})
it('gray on rgb(250, 248, 247) passes wcag aa for large text', () => {
assert(passesWcagAaLargeText('gray', 'rgb(250, 248, 247)'));
});
});
assert(passesWcagAaLargeText('gray', 'rgb(250, 248, 247)'))
})
})
describe('passesWcagAa', () => {
it('gray on rgb(250, 248, 247) fails wcag aa', () => {
assert.equal(false, passesWcagAa('gray', 'rgb(250, 248, 247)'));
});
assert.equal(false, passesWcagAa('gray', 'rgb(250, 248, 247)'))
})
it('black on darkgoldenrod passes wcag aa', () => {
assert(passesWcagAa('black', 'darkgoldenrod'));
});
});
assert(passesWcagAa('black', 'darkgoldenrod'))
})
})
describe('passesWcagAaa', () => {
it('black on darkgoldenrod fails wcag aaa', () => {
assert.equal(false, passesWcagAaa('black', 'darkgoldenrod'));
});
assert.equal(false, passesWcagAaa('black', 'darkgoldenrod'))
})
it('black on rgb(0, 0, 0) fails wcag aaa', () => {
assert.equal(false, passesWcagAaa('black', 'rgb(0, 0, 0)'));
});
assert.equal(false, passesWcagAaa('black', 'rgb(0, 0, 0)'))
})
it('rgb(255, 255, 255) on rgb(0, 0, 0) passes wcag aaa', () => {
assert.equal(false, passesWcagAaa('black', 'rgb(0, 0, 0)'));
});
});
});
assert.equal(false, passesWcagAaa('black', 'rgb(0, 0, 0)'))
})
})
})
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "node",
"declaration": true,
"outDir": "./build",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules", "test"]
}
Loading