@ac-kit/text-unicode 0.1.0 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,61 @@
1
1
  # @ac-kit/text-unicode
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [5c203e7]
8
+ - Updated dependencies [5c203e7]
9
+ - Updated dependencies [5c203e7]
10
+ - Updated dependencies [5c203e7]
11
+ - Updated dependencies [5c203e7]
12
+ - Updated dependencies [5c203e7]
13
+ - Updated dependencies [5c203e7]
14
+ - @ac-kit/algo@0.7.0
15
+ - @ac-kit/core@0.14.0
16
+
17
+ ## 0.2.0
18
+
19
+ ### Minor Changes
20
+
21
+ - 0b4fe43: `slugify` keeps its guarantee of a safe URL path segment and file name on every
22
+ platform. A `separator` or `fallback` with a character outside letters, digits
23
+ and `. _ ~ -`, or a `fallback` that is empty, over 255 characters, `.`, `..` or
24
+ ends with a dot, throws `RangeError`. The result is never empty (the `fallback`
25
+ now defaults to `"untitled"` instead of `""`) and never over 255 bytes, cut back
26
+ to its last letter or digit. A name that Windows reserves as a device name
27
+ (`con`, `prn`, `aux`, `nul`, `com0` to `com9`, `lpt0` to `lpt9`, with or without
28
+ an extension) gets an `_` after that name, in the first word before it is joined
29
+ and in the final result, after any cut, which shortens what follows the name
30
+ rather than drop the `_`: `slugify("CON")` is `"con_"`, `slugify("CoN.txt")` is
31
+ `"con_-txt"`.
32
+
33
+ ### Patch Changes
34
+
35
+ - Updated dependencies [0b4fe43]
36
+ - Updated dependencies [0b4fe43]
37
+ - Updated dependencies [0b4fe43]
38
+ - Updated dependencies [0b4fe43]
39
+ - Updated dependencies [0b4fe43]
40
+ - Updated dependencies [0b4fe43]
41
+ - Updated dependencies [0b4fe43]
42
+ - Updated dependencies [0b4fe43]
43
+ - Updated dependencies [0b4fe43]
44
+ - Updated dependencies [0b4fe43]
45
+ - Updated dependencies [0b4fe43]
46
+ - Updated dependencies [0b4fe43]
47
+ - Updated dependencies [0b4fe43]
48
+ - Updated dependencies [0b4fe43]
49
+ - Updated dependencies [0b4fe43]
50
+ - Updated dependencies [0b4fe43]
51
+ - Updated dependencies [0b4fe43]
52
+ - Updated dependencies [0b4fe43]
53
+ - Updated dependencies [0b4fe43]
54
+ - Updated dependencies [0b4fe43]
55
+ - Updated dependencies [0b4fe43]
56
+ - @ac-kit/algo@0.6.0
57
+ - @ac-kit/core@0.13.0
58
+
3
59
  ## 0.1.0
4
60
 
5
61
  ### Minor Changes
package/README.md CHANGED
@@ -37,7 +37,8 @@ slugify("Crème brûlée"); // "creme-brulee"
37
37
  collapsed, length capped.
38
38
  - `stripDiacritics` folds accented letters onto their base letters, and
39
39
  `slugify` makes a lowercase ASCII slug, safe as a URL segment and a file
40
- name.
40
+ name on every platform (it refuses an unsafe `separator` or `fallback` and
41
+ suffixes the names Windows reserves).
41
42
 
42
43
  See the
43
44
  [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/text-unicode/)
@@ -1,7 +1,7 @@
1
1
  export type SlugifyOptions = {
2
2
  /** Joins the retained runs. Defaults to `"-"`. */
3
3
  separator?: string | null;
4
- /** Returned when no alphanumeric character survives. Defaults to `""`. */
4
+ /** Returned when no alphanumeric character survives. Defaults to `"untitled"`. */
5
5
  fallback?: string | null;
6
6
  };
7
7
  /**
@@ -10,11 +10,27 @@ export type SlugifyOptions = {
10
10
  * trailing separator.
11
11
  *
12
12
  * The result is safe both as a URL path segment and as a file name on every
13
- * platform, since only `a-z`, `0-9` and `separator` survive.
13
+ * platform: only `a-z`, `0-9` and `separator` survive, `separator` and
14
+ * `fallback` are made of `A-Z`, `a-z`, `0-9`, `.`, `_`, `~` and `-` only, the
15
+ * result is never empty and never longer than 255 bytes, and a name that
16
+ * Windows reserves as a device name (`con`, `prn`, `aux`, `nul`, `com0` to
17
+ * `com9`, `lpt0` to `lpt9`, with or without an extension) gets an `_` after
18
+ * that name: the first word before it is joined (`CoN.txt` gives `con_-txt`,
19
+ * and `con_.txt` with the separator `.`), and the whole result again (`c o n`
20
+ * with an empty separator gives `con_`; a `fallback` of `con.txt` gives
21
+ * `con_.txt`). A longer slug is cut at 255 characters, which are 255 bytes and
22
+ * whole grapheme clusters, as every character is ASCII, then back to its last
23
+ * letter or digit; a reserved name is looked for after that cut, and when its
24
+ * `_` does not fit, what follows the name is cut instead, so a cut never gives
25
+ * back a reserved name (`con` followed by 251 dots and `x` gives `con_`).
14
26
  *
15
27
  * Letters Unicode defines no decomposition for (`Æ`, `Ø`, `Ł`, and every
16
28
  * non-Latin script) have no ASCII base to fold onto and are dropped with the
17
29
  * rest — a name written entirely in such letters yields `fallback`.
30
+ *
31
+ * @throws {RangeError} When `separator` or `fallback` has a character outside
32
+ * that set, or `fallback` is empty, longer than 255 characters, `.`, `..` or
33
+ * ends with a dot (Windows drops it).
18
34
  */
19
35
  export declare function slugify(input: string, options?: SlugifyOptions): string;
20
36
  //# sourceMappingURL=slugify.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"slugify.d.ts","sourceRoot":"","sources":["../../src/display/slugify.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,GAAG;IAC5B,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB,CAAC;AAIF;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,MAAM,CAWvE"}
1
+ {"version":3,"file":"slugify.d.ts","sourceRoot":"","sources":["../../src/display/slugify.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,cAAc,GAAG;IAC5B,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,kFAAkF;IAClF,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB,CAAC;AAcF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,MAAM,CAoCvE"}
@@ -1,25 +1,103 @@
1
+ import { describeValue, isWin32DeviceName } from "@ac-kit/core";
1
2
  import { stripDiacritics } from "../fold/strip-diacritics.js";
2
3
  const NON_SLUG_RUN = /[^a-z0-9]+/;
4
+ // The characters of a URL path segment that no platform's file system reads as
5
+ // anything else (RFC 3986 unreserved): no separator, wildcard, stream marker,
6
+ // control, space or non-ASCII character.
7
+ const SAFE_AFFIX = /^[A-Za-z0-9._~-]*$/;
8
+ // The longest file name, in bytes, that common file systems accept.
9
+ const MAXIMUM_LENGTH = 255;
10
+ const DEFAULT_FALLBACK = "untitled";
11
+ const TRAILING_NON_ALPHANUMERIC = /[^A-Za-z0-9]+$/;
3
12
  /**
4
13
  * Lowercase ASCII slug of `input`: diacritics folded onto their base letters,
5
14
  * every other run of characters collapsed into `separator`, and no leading or
6
15
  * trailing separator.
7
16
  *
8
17
  * The result is safe both as a URL path segment and as a file name on every
9
- * platform, since only `a-z`, `0-9` and `separator` survive.
18
+ * platform: only `a-z`, `0-9` and `separator` survive, `separator` and
19
+ * `fallback` are made of `A-Z`, `a-z`, `0-9`, `.`, `_`, `~` and `-` only, the
20
+ * result is never empty and never longer than 255 bytes, and a name that
21
+ * Windows reserves as a device name (`con`, `prn`, `aux`, `nul`, `com0` to
22
+ * `com9`, `lpt0` to `lpt9`, with or without an extension) gets an `_` after
23
+ * that name: the first word before it is joined (`CoN.txt` gives `con_-txt`,
24
+ * and `con_.txt` with the separator `.`), and the whole result again (`c o n`
25
+ * with an empty separator gives `con_`; a `fallback` of `con.txt` gives
26
+ * `con_.txt`). A longer slug is cut at 255 characters, which are 255 bytes and
27
+ * whole grapheme clusters, as every character is ASCII, then back to its last
28
+ * letter or digit; a reserved name is looked for after that cut, and when its
29
+ * `_` does not fit, what follows the name is cut instead, so a cut never gives
30
+ * back a reserved name (`con` followed by 251 dots and `x` gives `con_`).
10
31
  *
11
32
  * Letters Unicode defines no decomposition for (`Æ`, `Ø`, `Ł`, and every
12
33
  * non-Latin script) have no ASCII base to fold onto and are dropped with the
13
34
  * rest — a name written entirely in such letters yields `fallback`.
35
+ *
36
+ * @throws {RangeError} When `separator` or `fallback` has a character outside
37
+ * that set, or `fallback` is empty, longer than 255 characters, `.`, `..` or
38
+ * ends with a dot (Windows drops it).
14
39
  */
15
40
  export function slugify(input, options) {
41
+ const separator = options?.separator ?? "-";
42
+ const fallback = options?.fallback ?? DEFAULT_FALLBACK;
43
+ if (!SAFE_AFFIX.test(separator)) {
44
+ throw new RangeError(`separator must only contain letters, digits and . _ ~ -, got ${describeValue(separator)}`);
45
+ }
46
+ if (fallback === "" ||
47
+ fallback.length > MAXIMUM_LENGTH ||
48
+ !SAFE_AFFIX.test(fallback) ||
49
+ fallback === "." ||
50
+ fallback === ".." ||
51
+ fallback.endsWith(".")) {
52
+ throw new RangeError(`fallback must be a file name of 1 to ${MAXIMUM_LENGTH} letters, digits and . _ ~ -, not ending with a dot, got ${describeValue(fallback)}`);
53
+ }
16
54
  const parts = stripDiacritics(input)
17
55
  .toLowerCase()
18
56
  .split(NON_SLUG_RUN)
19
57
  .filter((part) => part !== "");
20
- if (parts.length === 0) {
21
- return options?.fallback ?? "";
58
+ if (parts.length > 0 && isWin32DeviceName(parts[0])) {
59
+ parts[0] += "_";
60
+ }
61
+ const slug = parts.length === 0
62
+ ? fallback
63
+ : joinUpTo_(parts, separator, MAXIMUM_LENGTH + 1);
64
+ return unreserve_(slug.length > MAXIMUM_LENGTH ? cut_(slug, MAXIMUM_LENGTH) : slug);
65
+ }
66
+ // The first `limit` characters of `parts.join(separator)` (all of it when
67
+ // shorter), without ever building the part of the join past them.
68
+ function joinUpTo_(parts, separator, limit) {
69
+ let joined = "";
70
+ for (const [index, part] of parts.entries()) {
71
+ if (index > 0) {
72
+ joined += separator.slice(0, limit - joined.length);
73
+ }
74
+ joined += part.slice(0, limit - joined.length);
75
+ if (joined.length >= limit) {
76
+ break;
77
+ }
22
78
  }
23
- return parts.join(options?.separator ?? "-");
79
+ return joined;
80
+ }
81
+ /**
82
+ * The last step: whatever the cut left, a reserved name gets its `_`, and only
83
+ * what follows the name is cut to make room for it, so the `_` itself always
84
+ * survives and no later step can bring the reserved name back.
85
+ */
86
+ function unreserve_(slug) {
87
+ if (!isWin32DeviceName(slug)) {
88
+ return slug;
89
+ }
90
+ const dot = slug.indexOf(".");
91
+ const name = dot === -1 ? slug : slug.slice(0, dot);
92
+ const marked = `${name}_`;
93
+ const rest = slug.slice(name.length);
94
+ return marked.length + rest.length > MAXIMUM_LENGTH
95
+ ? marked + cut_(rest, MAXIMUM_LENGTH - marked.length)
96
+ : marked + rest;
97
+ }
98
+ // Every character of a slug or a fallback is ASCII: one byte, one code point,
99
+ // one grapheme cluster, so a cut at a length is a cut at a cluster boundary.
100
+ function cut_(text, length) {
101
+ return text.slice(0, length).replace(TRAILING_NON_ALPHANUMERIC, "");
24
102
  }
25
103
  //# sourceMappingURL=slugify.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"slugify.js","sourceRoot":"","sources":["../../src/display/slugify.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAS9D,MAAM,YAAY,GAAG,YAAY,CAAC;AAElC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAAC,KAAa,EAAE,OAAwB;IAC9D,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC;SAClC,WAAW,EAAE;SACb,KAAK,CAAC,YAAY,CAAC;SACnB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;IAEhC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,OAAO,EAAE,QAAQ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CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,SAAS,UAAU,CAAC,IAAY;IAC/B,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAC;IACb,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,IAAI,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACpD,MAAM,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC;IAC1B,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACrC,OAAO,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,GAAG,cAAc;QAClD,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC;QACrD,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC;AAClB,CAAC;AAED,8EAA8E;AAC9E,6EAA6E;AAC7E,SAAS,IAAI,CAAC,IAAY,EAAE,MAAc;IACzC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAC;AACrE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/text-unicode",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Text by Unicode character properties and UAX algorithms: first strong direction (UAX #9), East Asian width (UAX #11), display-text normalisation, diacritic stripping and slugs.",
5
5
  "keywords": [
6
6
  "bidi",
@@ -47,16 +47,16 @@
47
47
  "prepack": "yarn build"
48
48
  },
49
49
  "dependencies": {
50
- "@ac-kit/algo": "^0.5.0",
51
- "@ac-kit/core": "^0.12.0",
50
+ "@ac-kit/algo": "^0.7.0",
51
+ "@ac-kit/core": "^0.14.0",
52
52
  "@types/web": "^0.0.367"
53
53
  },
54
54
  "devDependencies": {
55
- "@ac-kit/codegen-util": "0.1.0",
56
- "@ac-kit/math-random": "0.4.0",
55
+ "@ac-kit/codegen-util": "0.1.2",
56
+ "@ac-kit/math-random": "0.4.2",
57
57
  "@ac-kit/tsconfig": "0.1.0",
58
58
  "@ac-kit/typedoc-config": "0.1.0",
59
- "@ac-kit/vitest-config": "0.2.0",
59
+ "@ac-kit/vitest-config": "0.3.0",
60
60
  "@typescript/native": "typescript@7.0.2",
61
61
  "rimraf": "6.1.4",
62
62
  "tsx": "4.23.15",