react-native-jieba 0.2.0 → 0.3.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/Jieba.podspec +2 -0
  3. package/README.md +28 -13
  4. package/android/CMakeLists.txt +2 -0
  5. package/android/generated/java/com/jieba/NativeJiebaSpec.java +4 -0
  6. package/android/generated/jni/JiebaSpec-generated.cpp +6 -0
  7. package/android/generated/jni/react/renderer/components/JiebaSpec/JiebaSpecJSI.h +8 -0
  8. package/android/src/main/java/com/jieba/JiebaAndroidHelperModule.kt +7 -22
  9. package/android/src/main/java/com/jieba/JiebaDict.kt +72 -0
  10. package/android/src/main/java/com/jieba/JiebaPackage.kt +4 -0
  11. package/cpp/JiebaDictAndroid.cpp +26 -0
  12. package/cpp/JiebaDictAndroid.h +14 -0
  13. package/cpp/JiebaImpl.cpp +23 -0
  14. package/cpp/JiebaImpl.h +1 -0
  15. package/cpp/cppjieba/CHANGELOG.md +18 -0
  16. package/cpp/cppjieba/README.md +288 -0
  17. package/cpp/cppjieba/deps/limonp/CHANGELOG.md +175 -0
  18. package/cpp/cppjieba/deps/limonp/README.md +43 -0
  19. package/cpp/cppjieba/dict/README.md +62 -0
  20. package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec-generated.mm +7 -0
  21. package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec.h +1 -0
  22. package/ios/generated/ReactCodegen/JiebaSpecJSI.h +8 -0
  23. package/lib/module/NativeJieba.js.map +1 -1
  24. package/lib/module/NativeJieba.web.js +8 -2
  25. package/lib/module/NativeJieba.web.js.map +1 -1
  26. package/lib/module/index.js +1 -1
  27. package/lib/module/index.js.map +1 -1
  28. package/lib/module/init.js +33 -6
  29. package/lib/module/init.js.map +1 -1
  30. package/lib/module/init.web.js +19 -7
  31. package/lib/module/init.web.js.map +1 -1
  32. package/lib/typescript/src/NativeJieba.d.ts +1 -0
  33. package/lib/typescript/src/NativeJieba.d.ts.map +1 -1
  34. package/lib/typescript/src/NativeJieba.web.d.ts +1 -0
  35. package/lib/typescript/src/NativeJieba.web.d.ts.map +1 -1
  36. package/lib/typescript/src/index.d.ts +2 -1
  37. package/lib/typescript/src/index.d.ts.map +1 -1
  38. package/lib/typescript/src/init.d.ts +25 -2
  39. package/lib/typescript/src/init.d.ts.map +1 -1
  40. package/lib/typescript/src/init.web.d.ts +17 -3
  41. package/lib/typescript/src/init.web.d.ts.map +1 -1
  42. package/package.json +6 -1
  43. package/src/NativeJieba.ts +1 -0
  44. package/src/NativeJieba.web.ts +11 -3
  45. package/src/index.tsx +2 -1
  46. package/src/init.ts +35 -7
  47. package/src/init.web.ts +30 -9
@@ -1 +1 @@
1
- {"version":3,"names":["cut","cutAll","cutForSearch","cutHMM","cutSmall","tag","extract","insertUserWord","find","initJieba"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SACEA,GAAG,EACHC,MAAM,EACNC,YAAY,EACZC,MAAM,EACNC,QAAQ,EACRC,GAAG,EACHC,OAAO,EACPC,cAAc,EACdC,IAAI,QACC,YAAS;AAEhB,SAASC,SAAS,QAAQ,QAAQ","ignoreList":[]}
1
+ {"version":3,"names":["cut","cutAll","cutForSearch","cutHMM","cutSmall","tag","extract","insertUserWord","find","prepareJieba","isJiebaReady"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SACEA,GAAG,EACHC,MAAM,EACNC,YAAY,EACZC,MAAM,EACNC,QAAQ,EACRC,GAAG,EACHC,OAAO,EACPC,cAAc,EACdC,IAAI,QACC,YAAS;AAEhB,SAASC,YAAY,EAAEC,YAAY,QAAQ,QAAQ","ignoreList":[]}
@@ -3,10 +3,26 @@
3
3
  import { Platform } from 'react-native';
4
4
  import Jieba from './NativeJieba';
5
5
  import JiebaAndroidHelper from "./NativeJiebaAndroidHelper.js";
6
- let initPromise = null;
7
- export function initJieba(_options = {}) {
8
- if (initPromise) return initPromise;
9
- initPromise = (async () => {
6
+ let preparePromise = null;
7
+
8
+ /**
9
+ * Optional on native (iOS/Android): the dictionary is located/extracted
10
+ * automatically on the first segmentation call, so you can call `cut`, `tag`,
11
+ * etc. directly without ever calling `prepareJieba`.
12
+ *
13
+ * Calling it is still useful as a *warm-up*: on Android's very first run the
14
+ * dictionary assets are extracted from the APK, and doing that work here
15
+ * (asynchronously, up front) avoids a one-time blocking pause on the first
16
+ * `cut`/`tag` call.
17
+ *
18
+ * On web it is **required** — it loads and instantiates the `jieba-wasm`
19
+ * binary, which cannot be done synchronously.
20
+ *
21
+ * Idempotent: repeated calls return the same promise.
22
+ */
23
+ export function prepareJieba(_options = {}) {
24
+ if (preparePromise) return preparePromise;
25
+ preparePromise = (async () => {
10
26
  if (Platform.OS === 'android') {
11
27
  if (!JiebaAndroidHelper) {
12
28
  throw new Error('react-native-jieba: JiebaAndroidHelper native module is missing.');
@@ -16,9 +32,20 @@ export function initJieba(_options = {}) {
16
32
  }
17
33
  // iOS: OnLoad.mm already configured the dict path from the bundle.
18
34
  })().catch(err => {
19
- initPromise = null;
35
+ preparePromise = null;
20
36
  throw err;
21
37
  });
22
- return initPromise;
38
+ return preparePromise;
39
+ }
40
+
41
+ /**
42
+ * Whether jieba is ready to segment synchronously.
43
+ *
44
+ * Backed by the native engine state (not by whether `prepareJieba` was called),
45
+ * so it stays correct even when the dictionary is resolved lazily on the first
46
+ * `cut`/`tag` call. On iOS this is `true` from app launch.
47
+ */
48
+ export function isJiebaReady() {
49
+ return Jieba.isReady();
23
50
  }
24
51
  //# sourceMappingURL=init.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["Platform","Jieba","JiebaAndroidHelper","initPromise","initJieba","_options","OS","Error","path","prepareDictDir","setDictPath","catch","err"],"sourceRoot":"../../src","sources":["init.ts"],"mappings":";;AAAA,SAASA,QAAQ,QAAQ,cAAc;AACvC,OAAOC,KAAK,MAAM,eAAe;AACjC,OAAOC,kBAAkB,MAAM,+BAA4B;AAW3D,IAAIC,WAAiC,GAAG,IAAI;AAE5C,OAAO,SAASC,SAASA,CAACC,QAA0B,GAAG,CAAC,CAAC,EAAiB;EACxE,IAAIF,WAAW,EAAE,OAAOA,WAAW;EACnCA,WAAW,GAAG,CAAC,YAAY;IACzB,IAAIH,QAAQ,CAACM,EAAE,KAAK,SAAS,EAAE;MAC7B,IAAI,CAACJ,kBAAkB,EAAE;QACvB,MAAM,IAAIK,KAAK,CACb,kEACF,CAAC;MACH;MACA,MAAMC,IAAI,GAAG,MAAMN,kBAAkB,CAACO,cAAc,CAAC,CAAC;MACtDR,KAAK,CAACS,WAAW,CAACF,IAAI,CAAC;IACzB;IACA;EACF,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBT,WAAW,GAAG,IAAI;IAClB,MAAMS,GAAG;EACX,CAAC,CAAC;EACF,OAAOT,WAAW;AACpB","ignoreList":[]}
1
+ {"version":3,"names":["Platform","Jieba","JiebaAndroidHelper","preparePromise","prepareJieba","_options","OS","Error","path","prepareDictDir","setDictPath","catch","err","isJiebaReady","isReady"],"sourceRoot":"../../src","sources":["init.ts"],"mappings":";;AAAA,SAASA,QAAQ,QAAQ,cAAc;AACvC,OAAOC,KAAK,MAAM,eAAe;AACjC,OAAOC,kBAAkB,MAAM,+BAA4B;AAW3D,IAAIC,cAAoC,GAAG,IAAI;;AAE/C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAC1BC,QAA6B,GAAG,CAAC,CAAC,EACnB;EACf,IAAIF,cAAc,EAAE,OAAOA,cAAc;EACzCA,cAAc,GAAG,CAAC,YAAY;IAC5B,IAAIH,QAAQ,CAACM,EAAE,KAAK,SAAS,EAAE;MAC7B,IAAI,CAACJ,kBAAkB,EAAE;QACvB,MAAM,IAAIK,KAAK,CACb,kEACF,CAAC;MACH;MACA,MAAMC,IAAI,GAAG,MAAMN,kBAAkB,CAACO,cAAc,CAAC,CAAC;MACtDR,KAAK,CAACS,WAAW,CAACF,IAAI,CAAC;IACzB;IACA;EACF,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBT,cAAc,GAAG,IAAI;IACrB,MAAMS,GAAG;EACX,CAAC,CAAC;EACF,OAAOT,cAAc;AACvB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASU,YAAYA,CAAA,EAAY;EACtC,OAAOZ,KAAK,CAACa,OAAO,CAAC,CAAC;AACxB","ignoreList":[]}
@@ -1,10 +1,17 @@
1
1
  "use strict";
2
2
 
3
- import { setJiebaWasm } from "./NativeJieba.web.js";
4
- let initPromise = null;
5
- export function initJieba(options = {}) {
6
- if (initPromise) return initPromise;
7
- initPromise = (async () => {
3
+ import { setJiebaWasm, isJiebaWasmReady } from "./NativeJieba.web.js";
4
+ let preparePromise = null;
5
+
6
+ /**
7
+ * **Required on web**: loads and instantiates the `jieba-wasm` binary (which
8
+ * cannot be done synchronously). Await it once before calling `cut`, `tag`, etc.
9
+ *
10
+ * Idempotent: repeated calls return the same promise.
11
+ */
12
+ export function prepareJieba(options = {}) {
13
+ if (preparePromise) return preparePromise;
14
+ preparePromise = (async () => {
8
15
  // `jieba-wasm` resolves to its `web` build under the `browser`/`import`
9
16
  // conditions, whose default export is an async initializer that fetches
10
17
  // and instantiates the wasm binary.
@@ -20,9 +27,14 @@ export function initJieba(options = {}) {
20
27
  await wasm.default(options.wasmUrl);
21
28
  setJiebaWasm(wasm);
22
29
  })().catch(err => {
23
- initPromise = null;
30
+ preparePromise = null;
24
31
  throw err;
25
32
  });
26
- return initPromise;
33
+ return preparePromise;
34
+ }
35
+
36
+ /** Whether the wasm module has been loaded and jieba can segment. */
37
+ export function isJiebaReady() {
38
+ return isJiebaWasmReady();
27
39
  }
28
40
  //# sourceMappingURL=init.web.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["setJiebaWasm","initPromise","initJieba","options","wasm","default","wasmUrl","catch","err"],"sourceRoot":"../../src","sources":["init.web.ts"],"mappings":";;AAAA,SAASA,YAAY,QAA8B,sBAAmB;AAKtE,IAAIC,WAAiC,GAAG,IAAI;AAE5C,OAAO,SAASC,SAASA,CAACC,OAAyB,GAAG,CAAC,CAAC,EAAiB;EACvE,IAAIF,WAAW,EAAE,OAAOA,WAAW;EACnCA,WAAW,GAAG,CAAC,YAAY;IACzB;IACA;IACA;IACA,MAAMG,IAAI,GAAI,MAAM,MAAM,CAAC,YAAY,CAEpB;;IAEnB;IACA;IACA;IACA;IACA;IACA;IACA;IACA,MAAMA,IAAI,CAACC,OAAO,CAACF,OAAO,CAACG,OAAO,CAAC;IACnCN,YAAY,CAACI,IAAI,CAAC;EACpB,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBP,WAAW,GAAG,IAAI;IAClB,MAAMO,GAAG;EACX,CAAC,CAAC;EACF,OAAOP,WAAW;AACpB","ignoreList":[]}
1
+ {"version":3,"names":["setJiebaWasm","isJiebaWasmReady","preparePromise","prepareJieba","options","wasm","default","wasmUrl","catch","err","isJiebaReady"],"sourceRoot":"../../src","sources":["init.web.ts"],"mappings":";;AAAA,SACEA,YAAY,EACZC,gBAAgB,QAEX,sBAAmB;AAW1B,IAAIC,cAAoC,GAAG,IAAI;;AAE/C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAACC,OAA4B,GAAG,CAAC,CAAC,EAAiB;EAC7E,IAAIF,cAAc,EAAE,OAAOA,cAAc;EACzCA,cAAc,GAAG,CAAC,YAAY;IAC5B;IACA;IACA;IACA,MAAMG,IAAI,GAAI,MAAM,MAAM,CAAC,YAAY,CAEpB;;IAEnB;IACA;IACA;IACA;IACA;IACA;IACA;IACA,MAAMA,IAAI,CAACC,OAAO,CAACF,OAAO,CAACG,OAAO,CAAC;IACnCP,YAAY,CAACK,IAAI,CAAC;EACpB,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBP,cAAc,GAAG,IAAI;IACrB,MAAMO,GAAG;EACX,CAAC,CAAC;EACF,OAAOP,cAAc;AACvB;;AAEA;AACA,OAAO,SAASQ,YAAYA,CAAA,EAAY;EACtC,OAAOT,gBAAgB,CAAC,CAAC;AAC3B","ignoreList":[]}
@@ -1,5 +1,6 @@
1
1
  import { type TurboModule } from 'react-native';
2
2
  export interface Spec extends TurboModule {
3
+ isReady(): boolean;
3
4
  setDictPath(path: string): void;
4
5
  cut(sentence: string, hmm: boolean): string[];
5
6
  cutAll(sentence: string): string[];
@@ -1 +1 @@
1
- {"version":3,"file":"NativeJieba.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IAC9C,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IACvD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzD,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC5D,OAAO,CACL,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACX,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC3C,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAC7B;;AAED,wBAA+D"}
1
+ {"version":3,"file":"NativeJieba.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,OAAO,IAAI,OAAO,CAAC;IACnB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IAC9C,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IACvD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzD,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC5D,OAAO,CACL,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACX,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC3C,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAC7B;;AAED,wBAA+D"}
@@ -10,6 +10,7 @@ export type JiebaWasmModule = {
10
10
  add_word(word: string, freq?: number | null, tag?: string | null): number;
11
11
  };
12
12
  export declare function setJiebaWasm(module: JiebaWasmModule): void;
13
+ export declare function isJiebaWasmReady(): boolean;
13
14
  declare const Jieba: Spec;
14
15
  export default Jieba;
15
16
  //# sourceMappingURL=NativeJieba.web.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"NativeJieba.web.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.web.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAQ1C,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAClD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAChC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAC7D,GAAG,CACD,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GACnB,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACxC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC;CAC3E,CAAC;AAIF,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAE1D;AAYD,QAAA,MAAM,KAAK,EAAE,IA4CZ,CAAC;AAEF,eAAe,KAAK,CAAC"}
1
+ {"version":3,"file":"NativeJieba.web.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.web.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAQ1C,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAClD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAChC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAC7D,GAAG,CACD,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GACnB,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACxC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC;CAC3E,CAAC;AAIF,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAE1D;AAED,wBAAgB,gBAAgB,IAAI,OAAO,CAE1C;AAaD,QAAA,MAAM,KAAK,EAAE,IA+CZ,CAAC;AAEF,eAAe,KAAK,CAAC"}
@@ -1,4 +1,5 @@
1
1
  export { cut, cutAll, cutForSearch, cutHMM, cutSmall, tag, extract, insertUserWord, find, } from './jieba';
2
2
  export type { Tagged, Keyword } from './jieba';
3
- export { initJieba } from './init';
3
+ export { prepareJieba, isJiebaReady } from './init';
4
+ export type { PrepareJiebaOptions } from './init';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,MAAM,EACN,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,GAAG,EACH,OAAO,EACP,cAAc,EACd,IAAI,GACL,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,MAAM,EACN,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,GAAG,EACH,OAAO,EACP,cAAc,EACd,IAAI,GACL,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACpD,YAAY,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC"}
@@ -1,4 +1,4 @@
1
- export type InitJiebaOptions = {
1
+ export type PrepareJiebaOptions = {
2
2
  /**
3
3
  * Web only: URL (or fetch input) for the jieba-wasm `.wasm` binary. Most
4
4
  * bundlers resolve it automatically, so this is only needed when the asset
@@ -6,5 +6,28 @@ export type InitJiebaOptions = {
6
6
  */
7
7
  wasmUrl?: string | URL | Request;
8
8
  };
9
- export declare function initJieba(_options?: InitJiebaOptions): Promise<void>;
9
+ /**
10
+ * Optional on native (iOS/Android): the dictionary is located/extracted
11
+ * automatically on the first segmentation call, so you can call `cut`, `tag`,
12
+ * etc. directly without ever calling `prepareJieba`.
13
+ *
14
+ * Calling it is still useful as a *warm-up*: on Android's very first run the
15
+ * dictionary assets are extracted from the APK, and doing that work here
16
+ * (asynchronously, up front) avoids a one-time blocking pause on the first
17
+ * `cut`/`tag` call.
18
+ *
19
+ * On web it is **required** — it loads and instantiates the `jieba-wasm`
20
+ * binary, which cannot be done synchronously.
21
+ *
22
+ * Idempotent: repeated calls return the same promise.
23
+ */
24
+ export declare function prepareJieba(_options?: PrepareJiebaOptions): Promise<void>;
25
+ /**
26
+ * Whether jieba is ready to segment synchronously.
27
+ *
28
+ * Backed by the native engine state (not by whether `prepareJieba` was called),
29
+ * so it stays correct even when the dictionary is resolved lazily on the first
30
+ * `cut`/`tag` call. On iOS this is `true` from app launch.
31
+ */
32
+ export declare function isJiebaReady(): boolean;
10
33
  //# sourceMappingURL=init.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../src/init.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC;CAClC,CAAC;AAIF,wBAAgB,SAAS,CAAC,QAAQ,GAAE,gBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkBxE"}
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../src/init.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC;CAClC,CAAC;AAIF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,GAAE,mBAAwB,GACjC,OAAO,CAAC,IAAI,CAAC,CAkBf;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC"}
@@ -1,4 +1,18 @@
1
- import type { InitJiebaOptions } from './init';
2
- export type { InitJiebaOptions } from './init';
3
- export declare function initJieba(options?: InitJiebaOptions): Promise<void>;
1
+ export type PrepareJiebaOptions = {
2
+ /**
3
+ * URL (or fetch input) for the jieba-wasm `.wasm` binary. Most bundlers
4
+ * resolve it automatically, so this is only needed when the asset is served
5
+ * from a custom location.
6
+ */
7
+ wasmUrl?: string | URL | Request;
8
+ };
9
+ /**
10
+ * **Required on web**: loads and instantiates the `jieba-wasm` binary (which
11
+ * cannot be done synchronously). Await it once before calling `cut`, `tag`, etc.
12
+ *
13
+ * Idempotent: repeated calls return the same promise.
14
+ */
15
+ export declare function prepareJieba(options?: PrepareJiebaOptions): Promise<void>;
16
+ /** Whether the wasm module has been loaded and jieba can segment. */
17
+ export declare function isJiebaReady(): boolean;
4
18
  //# sourceMappingURL=init.web.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"init.web.d.ts","sourceRoot":"","sources":["../../../src/init.web.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAE/C,YAAY,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAI/C,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAwBvE"}
1
+ {"version":3,"file":"init.web.d.ts","sourceRoot":"","sources":["../../../src/init.web.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC;CAClC,CAAC;AAIF;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC,CAwB7E;AAED,qEAAqE;AACrE,wBAAgB,YAAY,IAAI,OAAO,CAEtC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-jieba",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Native Chinese text segmentation for React Native, powered by cppjieba.",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -15,6 +15,9 @@
15
15
  "files": [
16
16
  "src",
17
17
  "lib",
18
+ "README.md",
19
+ "CHANGELOG.md",
20
+ "LICENSE",
18
21
  "android/build.gradle",
19
22
  "android/CMakeLists.txt",
20
23
  "android/generated",
@@ -23,6 +26,8 @@
23
26
  "ios/generated",
24
27
  "cpp/JiebaImpl.h",
25
28
  "cpp/JiebaImpl.cpp",
29
+ "cpp/JiebaDictAndroid.h",
30
+ "cpp/JiebaDictAndroid.cpp",
26
31
  "cpp/cppjieba/LICENSE",
27
32
  "cpp/cppjieba/include/cppjieba",
28
33
  "cpp/cppjieba/deps/limonp/LICENSE",
@@ -1,6 +1,7 @@
1
1
  import { TurboModuleRegistry, type TurboModule } from 'react-native';
2
2
 
3
3
  export interface Spec extends TurboModule {
4
+ isReady(): boolean;
4
5
  setDictPath(path: string): void;
5
6
  cut(sentence: string, hmm: boolean): string[];
6
7
  cutAll(sentence: string): string[];
@@ -3,7 +3,7 @@ import type { Spec } from './NativeJieba';
3
3
  // `jieba-wasm` exposes the same segmentation primitives as cppjieba, but a
4
4
  // smaller surface: cut / cutAll / cutForSearch / tag / add_word. The remaining
5
5
  // native methods are polyfilled on top of those where the semantics allow, and
6
- // throw otherwise. The wasm module is loaded lazily by `initJieba()` (see
6
+ // throw otherwise. The wasm module is loaded lazily by `prepareJieba()` (see
7
7
  // `init.web.ts`) and handed to this shim via `setJiebaWasm`.
8
8
 
9
9
  export type JiebaWasmModule = {
@@ -23,17 +23,25 @@ export function setJiebaWasm(module: JiebaWasmModule): void {
23
23
  wasm = module;
24
24
  }
25
25
 
26
+ export function isJiebaWasmReady(): boolean {
27
+ return wasm !== null;
28
+ }
29
+
26
30
  function getWasm(): JiebaWasmModule {
27
31
  if (!wasm) {
28
32
  throw new Error(
29
- 'react-native-jieba: jieba-wasm is not initialized. ' +
30
- 'Call (and await) initJieba() before any segmentation call.'
33
+ 'react-native-jieba: jieba-wasm is not loaded yet. ' +
34
+ 'On web you must call (and await) prepareJieba() before any ' +
35
+ 'segmentation call.'
31
36
  );
32
37
  }
33
38
  return wasm;
34
39
  }
35
40
 
36
41
  const Jieba: Spec = {
42
+ isReady() {
43
+ return isJiebaWasmReady();
44
+ },
37
45
  setDictPath() {
38
46
  // No-op on web: jieba-wasm bundles its own dictionary.
39
47
  },
package/src/index.tsx CHANGED
@@ -10,4 +10,5 @@ export {
10
10
  find,
11
11
  } from './jieba';
12
12
  export type { Tagged, Keyword } from './jieba';
13
- export { initJieba } from './init';
13
+ export { prepareJieba, isJiebaReady } from './init';
14
+ export type { PrepareJiebaOptions } from './init';
package/src/init.ts CHANGED
@@ -2,7 +2,7 @@ import { Platform } from 'react-native';
2
2
  import Jieba from './NativeJieba';
3
3
  import JiebaAndroidHelper from './NativeJiebaAndroidHelper';
4
4
 
5
- export type InitJiebaOptions = {
5
+ export type PrepareJiebaOptions = {
6
6
  /**
7
7
  * Web only: URL (or fetch input) for the jieba-wasm `.wasm` binary. Most
8
8
  * bundlers resolve it automatically, so this is only needed when the asset
@@ -11,11 +11,28 @@ export type InitJiebaOptions = {
11
11
  wasmUrl?: string | URL | Request;
12
12
  };
13
13
 
14
- let initPromise: Promise<void> | null = null;
14
+ let preparePromise: Promise<void> | null = null;
15
15
 
16
- export function initJieba(_options: InitJiebaOptions = {}): Promise<void> {
17
- if (initPromise) return initPromise;
18
- initPromise = (async () => {
16
+ /**
17
+ * Optional on native (iOS/Android): the dictionary is located/extracted
18
+ * automatically on the first segmentation call, so you can call `cut`, `tag`,
19
+ * etc. directly without ever calling `prepareJieba`.
20
+ *
21
+ * Calling it is still useful as a *warm-up*: on Android's very first run the
22
+ * dictionary assets are extracted from the APK, and doing that work here
23
+ * (asynchronously, up front) avoids a one-time blocking pause on the first
24
+ * `cut`/`tag` call.
25
+ *
26
+ * On web it is **required** — it loads and instantiates the `jieba-wasm`
27
+ * binary, which cannot be done synchronously.
28
+ *
29
+ * Idempotent: repeated calls return the same promise.
30
+ */
31
+ export function prepareJieba(
32
+ _options: PrepareJiebaOptions = {}
33
+ ): Promise<void> {
34
+ if (preparePromise) return preparePromise;
35
+ preparePromise = (async () => {
19
36
  if (Platform.OS === 'android') {
20
37
  if (!JiebaAndroidHelper) {
21
38
  throw new Error(
@@ -27,8 +44,19 @@ export function initJieba(_options: InitJiebaOptions = {}): Promise<void> {
27
44
  }
28
45
  // iOS: OnLoad.mm already configured the dict path from the bundle.
29
46
  })().catch((err) => {
30
- initPromise = null;
47
+ preparePromise = null;
31
48
  throw err;
32
49
  });
33
- return initPromise;
50
+ return preparePromise;
51
+ }
52
+
53
+ /**
54
+ * Whether jieba is ready to segment synchronously.
55
+ *
56
+ * Backed by the native engine state (not by whether `prepareJieba` was called),
57
+ * so it stays correct even when the dictionary is resolved lazily on the first
58
+ * `cut`/`tag` call. On iOS this is `true` from app launch.
59
+ */
60
+ export function isJiebaReady(): boolean {
61
+ return Jieba.isReady();
34
62
  }
package/src/init.web.ts CHANGED
@@ -1,13 +1,29 @@
1
- import { setJiebaWasm, type JiebaWasmModule } from './NativeJieba.web';
2
- import type { InitJiebaOptions } from './init';
1
+ import {
2
+ setJiebaWasm,
3
+ isJiebaWasmReady,
4
+ type JiebaWasmModule,
5
+ } from './NativeJieba.web';
3
6
 
4
- export type { InitJiebaOptions } from './init';
7
+ export type PrepareJiebaOptions = {
8
+ /**
9
+ * URL (or fetch input) for the jieba-wasm `.wasm` binary. Most bundlers
10
+ * resolve it automatically, so this is only needed when the asset is served
11
+ * from a custom location.
12
+ */
13
+ wasmUrl?: string | URL | Request;
14
+ };
5
15
 
6
- let initPromise: Promise<void> | null = null;
16
+ let preparePromise: Promise<void> | null = null;
7
17
 
8
- export function initJieba(options: InitJiebaOptions = {}): Promise<void> {
9
- if (initPromise) return initPromise;
10
- initPromise = (async () => {
18
+ /**
19
+ * **Required on web**: loads and instantiates the `jieba-wasm` binary (which
20
+ * cannot be done synchronously). Await it once before calling `cut`, `tag`, etc.
21
+ *
22
+ * Idempotent: repeated calls return the same promise.
23
+ */
24
+ export function prepareJieba(options: PrepareJiebaOptions = {}): Promise<void> {
25
+ if (preparePromise) return preparePromise;
26
+ preparePromise = (async () => {
11
27
  // `jieba-wasm` resolves to its `web` build under the `browser`/`import`
12
28
  // conditions, whose default export is an async initializer that fetches
13
29
  // and instantiates the wasm binary.
@@ -25,8 +41,13 @@ export function initJieba(options: InitJiebaOptions = {}): Promise<void> {
25
41
  await wasm.default(options.wasmUrl);
26
42
  setJiebaWasm(wasm);
27
43
  })().catch((err) => {
28
- initPromise = null;
44
+ preparePromise = null;
29
45
  throw err;
30
46
  });
31
- return initPromise;
47
+ return preparePromise;
48
+ }
49
+
50
+ /** Whether the wasm module has been loaded and jieba can segment. */
51
+ export function isJiebaReady(): boolean {
52
+ return isJiebaWasmReady();
32
53
  }