segment-web.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  19. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  20. if (ar || !(i in from)) {
  21. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  22. ar[i] = from[i];
  23. }
  24. }
  25. return to.concat(ar || Array.prototype.slice.call(from));
  26. };
  27. Object.defineProperty(exports, "__esModule", { value: true });
  28. exports.segment = void 0;
  29. var segmentit_1 = require("segmentit");
  30. var segmentit;
  31. var hansIntlSegmenter;
  32. function segment(hans, segment) {
  33. if (segment === "segmentit") {
  34. if (!segmentit) {
  35. segmentit = (0, segmentit_1.useDefault)(new segmentit_1.Segment());
  36. }
  37. return segmentit.doSegment(hans, {
  38. simple: true,
  39. });
  40. }
  41. if (segment === "Intl.Segmenter") {
  42. if (typeof (Intl === null || Intl === void 0 ? void 0 : Intl.Segmenter) === "function") {
  43. if (!hansIntlSegmenter) {
  44. hansIntlSegmenter = new Intl.Segmenter("zh-Hans-CN", {
  45. granularity: "word",
  46. });
  47. }
  48. return __spreadArray([], __read(hansIntlSegmenter.segment(hans)), false).map(function (s) { return s.segment; });
  49. }
  50. }
  51. return [hans];
  52. }
  53. exports.segment = segment;
  54. //# sourceMappingURL=segment-web.js.map