i18nizeelement 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,24 @@
1
1
  {
2
2
  "name": "i18nizeelement",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "author": "Brett Zamir",
5
5
  "contributors": [],
6
6
  "license": "MIT",
7
7
  "description": "Internationalize an element with `lang`, `dir`, and `style.writingMode` as indicated by the supplied language and settings",
8
+ "type": "module",
8
9
  "main": "dist/i18nizeelement.umd.js",
9
- "module": "dist/i18nizeelement.es6.js",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/i18nizeelement.es6.js",
14
+ "require": "./dist/i18nizeelement.umd.js"
15
+ },
16
+ "./jamilih-plugin.js": {
17
+ "types": "./dist/i18nizeelement-jamilih-plugin.d.ts",
18
+ "import": "./dist/i18nizeelement-jamilih-plugin.es6.js",
19
+ "require": "./dist/i18nizeelement-jamilih-plugin.umd.js"
20
+ }
21
+ },
10
22
  "repository": {
11
23
  "type": "git",
12
24
  "url": "https://github.com/brettz9/i18nizeElement"
@@ -19,55 +31,60 @@
19
31
  "rtl"
20
32
  ],
21
33
  "scripts": {
34
+ "copy": "cp i18nizeelement-jamilih-plugin.es6.d.ts dist/i18nizeelement-jamilih-plugin.es6.d.ts",
35
+ "lint": "npm run eslint",
22
36
  "eslint": "eslint --ext=js,md,html .",
37
+ "tsc": "tsc",
38
+ "build": "npm run rollup && npm run copy && tsc -p tsconfig-prod.json",
23
39
  "rollup": "rollup -c rollup.config.umd.js && rollup -c rollup.config.es6.js",
24
40
  "start": "npm run test:watch",
25
- "mocha": "_mocha --require chai/register-expect --require esm --require test/bootstrap/node.js test/*.js --exit",
41
+ "mocha": "_mocha --require chai/register-expect.js --require test/bootstrap/node.js test/*.js --exit",
26
42
  "test:watch": "nodemon --watch src --watch test --exec 'npm test'",
27
- "nyc": "echo 'Deleting esm cache for coverage' && rm -Rf node_modules/.cache && nyc npm run mocha",
28
- "test": "npm run eslint && npm run rollup && npm run nyc",
29
- "browser-test": "npm run rollup && open-cli http://127.0.0.1:8080/test/ && static -p 8080",
30
- "prepublishOnly": "npm run rollup && npm test"
43
+ "c8": "echo 'Deleting esm cache for coverage' && rm -Rf node_modules/.cache && c8 npm run mocha",
44
+ "test": "npm run eslint && npm run build && npm run c8",
45
+ "browser-test": "npm run build && open-cli http://127.0.0.1:8080/test/ && static -p 8080",
46
+ "prepublishOnly": "npm run build && npm test"
31
47
  },
32
48
  "engines": {
33
- "node": ">=6.0.0"
49
+ "node": ">=16.0.0"
34
50
  },
35
51
  "devDependencies": {
36
- "@babel/core": "^7.11.1",
37
- "@mysticatea/eslint-plugin": "^13.0.0",
38
- "@rollup/plugin-babel": "^5.2.0",
39
- "@rollup/plugin-commonjs": "^15.0.0",
40
- "@rollup/plugin-node-resolve": "^9.0.0",
41
- "chai": "^4.2.0",
42
- "eslint": "^7.7.0",
43
- "eslint-config-ash-nazg": "^22.7.0",
44
- "eslint-config-standard": "^14.1.1",
45
- "eslint-plugin-array-func": "^3.1.7",
46
- "eslint-plugin-compat": "^3.8.0",
52
+ "@babel/core": "^7.21.8",
53
+ "@brettz9/eslint-plugin": "^1.0.4",
54
+ "@rollup/plugin-babel": "^6.0.3",
55
+ "@rollup/plugin-node-resolve": "^15.0.2",
56
+ "@types/chai": "^4.3.5",
57
+ "@types/jsdom": "^21.1.1",
58
+ "@types/mocha": "^10.0.1",
59
+ "c8": "^7.13.0",
60
+ "chai": "^4.3.7",
61
+ "eslint": "^8.40.0",
62
+ "eslint-config-ash-nazg": "^34.12.0",
63
+ "eslint-config-standard": "^17.0.0",
64
+ "eslint-plugin-array-func": "^3.1.8",
65
+ "eslint-plugin-compat": "^4.1.4",
47
66
  "eslint-plugin-eslint-comments": "^3.2.0",
48
- "eslint-plugin-html": "^6.0.3",
49
- "eslint-plugin-import": "^2.22.0",
50
- "eslint-plugin-jsdoc": "^30.2.2",
51
- "eslint-plugin-markdown": "^1.0.2",
52
- "eslint-plugin-no-unsanitized": "^3.1.2",
67
+ "eslint-plugin-html": "^7.1.0",
68
+ "eslint-plugin-import": "^2.27.5",
69
+ "eslint-plugin-jsdoc": "^44.2.1",
70
+ "eslint-plugin-markdown": "^3.0.0",
71
+ "eslint-plugin-no-unsanitized": "^4.0.2",
53
72
  "eslint-plugin-no-use-extend-native": "^0.5.0",
54
73
  "eslint-plugin-node": "^11.1.0",
55
- "eslint-plugin-promise": "^4.2.1",
56
- "eslint-plugin-sonarjs": "^0.5.0",
57
- "eslint-plugin-standard": "^4.0.1",
58
- "eslint-plugin-unicorn": "^21.0.0",
74
+ "eslint-plugin-promise": "^6.1.1",
75
+ "eslint-plugin-sonarjs": "^0.19.0",
76
+ "eslint-plugin-standard": "^4.1.0",
77
+ "eslint-plugin-unicorn": "^47.0.0",
59
78
  "esm": "^3.2.25",
60
- "jamilih": "^0.53.2",
61
- "jsdom": "^16.4.0",
62
- "mocha": "^8.1.1",
79
+ "intl-locale-textinfo-polyfill": "^1.0.1",
80
+ "jamilih": "^0.57.1",
81
+ "jsdom": "^22.0.0",
82
+ "mocha": "^10.2.0",
63
83
  "node-static": "0.7.11",
64
- "nodemon": "^2.0.4",
65
- "nyc": "^15.1.0",
66
- "open-cli": "^6.0.1",
67
- "rollup": "^2.26.0",
68
- "rtl-detect": "^1.0.2",
69
- "source-map-support": "^0.5.19",
70
- "typescript": "^3.9.7"
71
- },
72
- "dependencies": {}
84
+ "nodemon": "^2.0.22",
85
+ "open-cli": "^7.2.0",
86
+ "rollup": "^3.21.6",
87
+ "source-map-support": "^0.5.21",
88
+ "typescript": "^5.0.4"
89
+ }
73
90
  }
@@ -1,8 +1,12 @@
1
1
  import i18nizeElement from './index.js';
2
2
 
3
+ /** @type {import('jamilih').JamilihPlugin} */
3
4
  const plugin = {
4
5
  name: '$_language',
5
- async set ({element, attribute: {value: options}}) {
6
+ async set ({element, attribute: {value}}) {
7
+ let options = /** @type {import('jamilih').PluginValue} */ (
8
+ value
9
+ );
6
10
  if (typeof options === 'string') {
7
11
  options = {language: options};
8
12
  } else if (Array.isArray(options)) {
@@ -16,8 +20,15 @@ const plugin = {
16
20
  // the child element into the ancestors
17
21
  // it is being built with
18
22
  await Promise.resolve();
19
- i18nizeElement(element, {
20
- ...options
23
+ i18nizeElement(/** @type {HTMLElement} */ (element), {
24
+ ...(
25
+ /**
26
+ * @type {{
27
+ * [key: string]: any;
28
+ * }}
29
+ */
30
+ (options)
31
+ )
21
32
  });
22
33
  }
23
34
  };
package/src/index.js CHANGED
@@ -1,6 +1,16 @@
1
- import rtlDetect from 'rtl-detect';
2
- // https://github.com/shadiabuhilal/rtl-detect/issues/3
1
+ import 'intl-locale-textinfo-polyfill/lib/polyfill.js';
2
+
3
3
  // e.g., `i18nizeElement(document.title, {language: 'ar-AR'});`
4
+
5
+ /**
6
+ * @param {HTMLElement} element
7
+ * @param {{
8
+ * language?: string,
9
+ * avoidLangIfSet?: boolean,
10
+ * avoidDirIfSet?: boolean,
11
+ * avoidLTRByDefault?: boolean
12
+ * }} cfg
13
+ */
4
14
  const i18nizeElement = (element, {
5
15
  language,
6
16
  // These avoid setting if found to be already set with the same value
@@ -22,7 +32,9 @@ const i18nizeElement = (element, {
22
32
  // 3. The closest ancestor with `lang` has a different language from
23
33
  // the supplied
24
34
  if (avoidLangIfSet) {
25
- presetLangElement = element.closest('[lang]');
35
+ presetLangElement = /** @type {HTMLElement} */ (
36
+ element.closest('[lang]')
37
+ );
26
38
  }
27
39
  if (!presetLangElement || presetLangElement.lang !== language) {
28
40
  element.lang = language;
@@ -37,14 +49,25 @@ const i18nizeElement = (element, {
37
49
  // direction or the user isn't avoiding (the default) LTR)
38
50
  // 3. The closest ancestor with `dir` has a different `dir` from the
39
51
  // direction of the supplied language
40
- const dir = rtlDetect.getLangDir(language);
52
+ const {direction: dir} =
53
+ /**
54
+ * @type {Intl.Locale & {
55
+ * textInfo: {direction: "ltr"|"rtl"|"ttb"}
56
+ * }}
57
+ */ (
58
+ new Intl.Locale(language)
59
+ ).textInfo;
60
+
61
+ /** @type {HTMLElement|undefined} */
41
62
  let presetDirElement;
42
63
  if (avoidDirIfSet ||
43
64
  // If avoiding the default LTR except when different (even if not
44
65
  // avoiding an (RTL) already-set dir), we need to know if different
45
66
  (avoidLTRByDefault && dir === 'ltr')
46
67
  ) {
47
- presetDirElement = element.closest('[dir]');
68
+ presetDirElement = /** @type {HTMLElement} */ (
69
+ element.closest('[dir]')
70
+ );
48
71
  }
49
72
  if (
50
73
  // If the closest ancestor with `dir` is different, we need to
@@ -63,8 +86,8 @@ const i18nizeElement = (element, {
63
86
  ) {
64
87
  element.dir = dir;
65
88
 
66
- /* istanbul ignore next */
67
- if (dir === 'ttb') { // Assumes https://github.com/shadiabuhilal/rtl-detect/issues/2
89
+ /* c8 ignore next 3 */
90
+ if (dir === 'ttb') {
68
91
  element.style.writingMode = 'vertical-lr';
69
92
  }
70
93
  }
package/CHANGES.md DELETED
@@ -1,45 +0,0 @@
1
- # CHANGES for i18nizeelement
2
-
3
- ## 0.4.0
4
-
5
- - Docs: Update Jamilih API; document additional Jamilih plugin APIs
6
- - Linting (ESLint): As per latest ash-nazg
7
- - Testing: Use `chai/register-expect`
8
- - Testing: Fix paths to allow avoiding a global; drop now unneeded `closest`
9
- polyfill
10
- - npm: Add eslint to test script
11
- - npm: Script from `prepare` to `prepublishOnly`
12
- - npm: Update `rollup-plugin-babel` to `@rollup/plugin-babel`
13
- and make explicit `babelHelpers` value of `bundled`
14
- - npm: Switch to non-deprecated `@rollup/plugin-node-resolve`
15
- - npm: Switch to non-deprecated `@rollup/plugin-commonjs`
16
- - npm: Remove `test-cov` script in favor of letting `test` run coverage
17
- - npm: Delete full cache (ESM and nyc) in testing coverage
18
- - npm: Update devDeps. and package-lock
19
-
20
- ## 0.3.0
21
-
22
- - Breaking change: Insist on Node >= 6
23
- - Linting (ESLint): Apply ash-nazg; use a recommended extension ("js")
24
- - Testing: Move `tests` to `test`
25
- - Testing: Switch to `esm`
26
- - Testing: Add `nyc` for coverage and bring to 100%
27
- - Build: Switch to Babel; update Rollup API usage
28
- - Maintenance: Add `.editorconfig`
29
- - Docs: Add badges
30
- - npm: Update devDeps; avoid `build` script; `opn-cli`->`open-cli`
31
- - npm: Remove `rollup-watch` and `rollup-plugin-multi-entry`
32
-
33
- ## 0.2.1
34
-
35
- - Exclude items from `.npmignore`
36
-
37
- ## 0.2.0
38
-
39
- - Build: Add i18nizeElement Jamilih plugin builds, ensuring buble runs
40
- before other Rollup plugins
41
- - Refactoring: Rename plugin file
42
-
43
- ## 0.1.0
44
-
45
- - Initial release