Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
feat: add example and tests (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
fourdim authored Aug 27, 2023
1 parent 765abfe commit b87a7cd
Show file tree
Hide file tree
Showing 20 changed files with 2,018 additions and 188 deletions.
40 changes: 40 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
overrides: [
{
env: {
node: true,
},
files: ['.eslintrc.{js,cjs}'],
parserOptions: {
sourceType: 'script',
},
},
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'prettier'],
rules: {
'prettier/prettier': 'error',
'@typescript-eslint/ban-ts-comment': [
'error',
{
'ts-expect-error': 'allow-with-description',
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
},
],
},
};
39 changes: 38 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build Package
name: Build & Test

on:
push:
Expand Down Expand Up @@ -34,3 +34,40 @@ jobs:

- name: Build
run: sh build.sh

- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: latest
run_install: false

- name: Get pnpm store directory
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Use Node.js LTS
uses: actions/setup-node@v3
with:
node-version: 18
registry-url: https://npm.pkg.github.com
cache: 'pnpm'

- name: Install dependencies
run: pnpm install

- name: Lint
run: pnpm lint

- name: Install playwright browser
run: pnpm playwright install firefox

- name: Run playwright test
run: pnpm test -- --forbid-only
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ wasm-pack.log

node_modules/
data/
test-results/
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2,
"arrowParens": "avoid"
}
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ echo "$(jq --argjson FILES $(ls pkg | jq -R -s -c 'split("\n")[:-1]') \
.exports.".".import.default |= "./esm/intl_segmenter_polyfill_rs.mjs" |
.exports.".".require.types |= "./esm/intl_segmenter_polyfill_rs.d.ts" |
.exports.".".require.default |= "./cjs/intl_segmenter_polyfill_rs.js" |
.sideEffects[0] |= "./esm/intl_segmenter_polyfill_rs.js"' \
.sideEffects[0] |= "./esm/intl_segmenter_polyfill_rs.mjs"' \
pkg/package.json)" > pkg/package.json
4 changes: 2 additions & 2 deletions index.html → example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<script type="module" src="./index.js"></script>
<script type="module" src="./index.ts"></script>
</body>
</html>
</html>
72 changes: 72 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as IntlSegmeterPolyfill from 'intl-segmenter-polyfill-rs';

const segmenterFr = new IntlSegmeterPolyfill.Segmenter('fr', {
granularity: 'word',
});
const string1 = 'Que ma joie demeure';

const iterator1 = segmenterFr.segment(string1)[Symbol.iterator]();

console.log(iterator1.next().value.segment);
// Expected output: 'Que'

console.log(iterator1.next().value.segment);
// Expected output: ' '

// Browsers like firefox
if (Intl.Segmenter === undefined) {
Object.defineProperty(Intl, 'Segmenter', {
value: IntlSegmeterPolyfill.Segmenter,
configurable: true,
writable: true,
});
segmenterExample(true, 'sentence');
segmenterExample(true, 'word');
const spanNode2 = document.createElement('span');
spanNode2.innerText =
'You are using intl-segmenter-polyfill-rs polyfilled Intl.Segmenter.';
spanNode2.id = 'polyfill-status';
document.body.appendChild(spanNode2);
document.body.appendChild(document.createElement('br'));
} else {
segmenterExample(false, 'sentence');
segmenterExample(false, 'word');
const spanNode2 = document.createElement('span');
spanNode2.innerText =
'You are using intl-segmenter-polyfill-rs with no polyfill applied to your browser as itself support segmenter.';
spanNode2.id = 'polyfill-status';
document.body.appendChild(spanNode2);
document.body.appendChild(document.createElement('br'));
}

function segmenterExample(
polyfilled: boolean,
granularity: Intl.SegmenterOptions['granularity']
) {
const segmenterFr = new (polyfilled ? Intl : IntlSegmeterPolyfill).Segmenter(
'fr',
{ granularity }
);
const string = 'Hello World. Xin chào thế giới!';
const spanNode = document.createElement('span');
spanNode.innerText =
'Splitting "' + string + '" with granularity ' + granularity + ':';
document.body.appendChild(spanNode);
document.body.appendChild(document.createElement('br'));

const iterator = segmenterFr.segment(string)[Symbol.iterator]();

const array = Array.from(iterator).map(
ele => (ele as IntlSegmeterPolyfill.SegmentData).segment
);

const list = document.createElement('ol');
document.body.appendChild(list);
array.map((seg, index) => {
const spanNode = document.createElement('li');
spanNode.id = `segmenter-${granularity}-${index}`;
spanNode.innerText = seg !== ' ' ? seg : '\xa0';
list.appendChild(spanNode);
});
document.body.appendChild(document.createElement('br'));
}
19 changes: 19 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "intl-segmenter-polyfill-example",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"intl-segmenter-polyfill-rs": "file:../pkg"
},
"devDependencies": {
"@swc/core": "^1.3.80",
"vite": "^4.4.5",
"vite-plugin-wasm": "^3.2.2"
}
}
3 changes: 3 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends":"../tsconfig.json"
}
Loading

0 comments on commit b87a7cd

Please sign in to comment.