This repository has been archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
2,018 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ wasm-pack.log | |
|
||
node_modules/ | ||
data/ | ||
test-results/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends":"../tsconfig.json" | ||
} |
Oops, something went wrong.