probably-china 1.0.0 → 1.1.0

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/README.md CHANGED
@@ -10,4 +10,18 @@ based on the available browser info. It currently uses two strategies:
10
10
  1. Check if the current browser favors simplified Chinese or related languages.
11
11
  2. Check if the current timezone is one of the Chinese timezones.
12
12
 
13
- The use case for this package is not to avoid showing information that are sensitive in China.
13
+ The use case for this package is not to avoid showing information that are sensitive in China.
14
+
15
+ ## Usage
16
+
17
+ Install the package:
18
+
19
+ ```bash
20
+ pnpm add probably-china
21
+ ```
22
+
23
+ And it exports a single boolean constant:
24
+
25
+ ```js
26
+ import { probablyChina } from "probably-china";
27
+ ```
@@ -1,7 +1,6 @@
1
-
2
- /**
3
- * A lucky guess that we are probably in China or facing a Chinese user.
4
- */
5
- export const probablyChina: boolean;
6
-
7
- export default probablyChina;
1
+ /**
2
+ * A lucky guess that we are probably in China or facing a Chinese user.
3
+ */
4
+ export declare const probablyChina: boolean;
5
+
6
+ export default probablyChina;
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+
2
+ const detectChinaLanguage = ["zh-CN", "zh-CHS", "zh-Hans", "zh-HK", "zh-MO"];
3
+ const detectChinaTimeZone = ["Asia/Shanghai", "Asia/Urumqi", "Asia/Hong_Kong", "Asia/Macau"];
4
+
5
+ function checkTimeZone() {
6
+ if(typeof Intl !== "undefined") {
7
+ const timeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
8
+ return detectChinaTimeZone.indexOf(timeZone) >= 0;
9
+ } else {
10
+ // legacy browser
11
+ const str = new Date().toString();
12
+ return str.indexOf("\u4e2d") > 0 || str.indexOf("\u9999") > 0;
13
+ }
14
+ }
15
+
16
+ function checkChinaLanguages() {
17
+ if(navigator.languages) { // https://caniuse.com/mdn-api_navigator_languages
18
+ for(let i = 0; i < navigator.languages.length; i++) {
19
+ const l = navigator.languages[i];
20
+ // Some Taiwanese users might also have "zh-CN" in their language list.
21
+ // We use the following criteria to eliminate such false-positive,
22
+ // so that it returns false if "zh-TW" comes first in the language list.
23
+ if(l === "zh-TW") return false;
24
+ if(detectChinaLanguage.indexOf(l) >= 0) return true;
25
+ }
26
+ return false;
27
+ }
28
+ // legacy browser
29
+ return detectChinaLanguage.indexOf(navigator.language) >= 0;
30
+ }
31
+
32
+ /**
33
+ * A lucky guess that we are probably in China or facing a Chinese user.
34
+ */
35
+ export const probablyChina = checkTimeZone() || checkChinaLanguages();
36
+
37
+ export default probablyChina;
package/package.json CHANGED
@@ -1,13 +1,23 @@
1
1
  {
2
- "name": "probably-china",
3
- "version": "1.0.0",
4
- "description": "A lucky guess that we are probably in China or facing a Chinese user.",
5
- "main": "index.js",
6
- "types": "index.d.ts",
7
- "keywords": [
8
- "China"
9
- ],
10
- "author": "Mu-Tsun Tsai <don.m.t.tsai@gmail.com> (https://github.com/MuTsunTsai)",
11
- "repository": "github:MuTsunTsai/probably-china",
12
- "license": "MIT"
13
- }
2
+ "name": "probably-china",
3
+ "version": "1.1.0",
4
+ "description": "A lucky guess that we are probably in China or facing a Chinese user.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "keywords": [
8
+ "China"
9
+ ],
10
+ "author": "Mu-Tsun Tsai <don.m.t.tsai@gmail.com> (https://github.com/MuTsunTsai)",
11
+ "repository": "github:MuTsunTsai/probably-china",
12
+ "license": "MIT",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "devDependencies": {
17
+ "eslint": "^9.17.0",
18
+ "eslint-plugin-compat": "^6.0.2"
19
+ },
20
+ "scripts": {
21
+ "start": "pnpx http-server -o /test"
22
+ }
23
+ }
package/index.js DELETED
@@ -1,14 +0,0 @@
1
-
2
- const detectChinaLanguage = ["zh-CN", "zh-CHS", "zh-Hans", "zh-HK", "zh-MO"];
3
- const detectChinaTimeZone = ["Asia/Shanghai", "Asia/Urumqi", "Asia/Hong_Kong", "Asia/Macau"];
4
- const timeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
5
-
6
- /**
7
- * A lucky guess that we are probably in China or facing a Chinese user.
8
- */
9
- export const probablyChina =
10
- detectChinaTimeZone.includes(timeZone) ||
11
- detectChinaLanguage.includes(navigator.language) ||
12
- navigator.languages.some(l => detectChinaLanguage.includes(l));
13
-
14
- export default probablyChina;