@docusaurus/utils-common 3.1.0 → 3.2.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.
@@ -6,5 +6,10 @@
6
6
  */
7
7
  import type { DocusaurusConfig } from '@docusaurus/types';
8
8
  export type ApplyTrailingSlashParams = Pick<DocusaurusConfig, 'trailingSlash' | 'baseUrl'>;
9
+ export declare function addTrailingSlash(str: string): string;
9
10
  export default function applyTrailingSlash(path: string, options: ApplyTrailingSlashParams): string;
11
+ /** Appends a leading slash to `str`, if one doesn't exist. */
12
+ export declare function addLeadingSlash(str: string): string;
13
+ /** Removes the trailing slash from `str`. */
14
+ export declare function removeTrailingSlash(str: string): string;
10
15
  //# sourceMappingURL=applyTrailingSlash.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"applyTrailingSlash.d.ts","sourceRoot":"","sources":["../src/applyTrailingSlash.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AAExD,MAAM,MAAM,wBAAwB,GAAG,IAAI,CACzC,gBAAgB,EAChB,eAAe,GAAG,SAAS,CAC5B,CAAC;AAGF,MAAM,CAAC,OAAO,UAAU,kBAAkB,CACxC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,wBAAwB,GAChC,MAAM,CAsCR"}
1
+ {"version":3,"file":"applyTrailingSlash.d.ts","sourceRoot":"","sources":["../src/applyTrailingSlash.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AAExD,MAAM,MAAM,wBAAwB,GAAG,IAAI,CACzC,gBAAgB,EAChB,eAAe,GAAG,SAAS,CAC5B,CAAC;AAEF,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEpD;AAGD,MAAM,CAAC,OAAO,UAAU,kBAAkB,CACxC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,wBAAwB,GAChC,MAAM,CA+BR;AAED,8DAA8D;AAC9D,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,6CAA6C;AAC7C,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvD"}
@@ -6,6 +6,12 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.removeTrailingSlash = exports.addLeadingSlash = exports.addTrailingSlash = void 0;
10
+ const stringUtils_1 = require("./stringUtils");
11
+ function addTrailingSlash(str) {
12
+ return str.endsWith('/') ? str : `${str}/`;
13
+ }
14
+ exports.addTrailingSlash = addTrailingSlash;
9
15
  // Trailing slash handling depends in some site configuration options
10
16
  function applyTrailingSlash(path, options) {
11
17
  const { trailingSlash, baseUrl } = options;
@@ -13,13 +19,6 @@ function applyTrailingSlash(path, options) {
13
19
  // Never apply trailing slash to an anchor link
14
20
  return path;
15
21
  }
16
- // TODO deduplicate: also present in @docusaurus/utils
17
- function addTrailingSlash(str) {
18
- return str.endsWith('/') ? str : `${str}/`;
19
- }
20
- function removeTrailingSlash(str) {
21
- return str.endsWith('/') ? str.slice(0, -1) : str;
22
- }
23
22
  function handleTrailingSlash(str, trailing) {
24
23
  return trailing ? addTrailingSlash(str) : removeTrailingSlash(str);
25
24
  }
@@ -40,4 +39,14 @@ function applyTrailingSlash(path, options) {
40
39
  return path.replace(pathname, newPathname);
41
40
  }
42
41
  exports.default = applyTrailingSlash;
42
+ /** Appends a leading slash to `str`, if one doesn't exist. */
43
+ function addLeadingSlash(str) {
44
+ return (0, stringUtils_1.addPrefix)(str, '/');
45
+ }
46
+ exports.addLeadingSlash = addLeadingSlash;
47
+ /** Removes the trailing slash from `str`. */
48
+ function removeTrailingSlash(str) {
49
+ return (0, stringUtils_1.removeSuffix)(str, '/');
50
+ }
51
+ exports.removeTrailingSlash = removeTrailingSlash;
43
52
  //# sourceMappingURL=applyTrailingSlash.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"applyTrailingSlash.js","sourceRoot":"","sources":["../src/applyTrailingSlash.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AASH,qEAAqE;AACrE,SAAwB,kBAAkB,CACxC,IAAY,EACZ,OAAiC;IAEjC,MAAM,EAAC,aAAa,EAAE,OAAO,EAAC,GAAG,OAAO,CAAC;IAEzC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;QACxB,+CAA+C;QAC/C,OAAO,IAAI,CAAC;KACb;IAED,sDAAsD;IACtD,SAAS,gBAAgB,CAAC,GAAW;QACnC,OAAO,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;IAC7C,CAAC;IACD,SAAS,mBAAmB,CAAC,GAAW;QACtC,OAAO,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;IACpD,CAAC;IACD,SAAS,mBAAmB,CAAC,GAAW,EAAE,QAAiB;QACzD,OAAO,QAAQ,CAAC,CAAC,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC;IACrE,CAAC;IAED,8CAA8C;IAC9C,IAAI,OAAO,aAAa,KAAK,WAAW,EAAE;QACxC,OAAO,IAAI,CAAC;KACb;IAED,iEAAiE;IACjE,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAA0B,CAAC;IAE/D,4BAA4B;IAC5B,2CAA2C;IAC3C,oEAAoE;IACpE,uEAAuE;IACvE,MAAM,cAAc,GAAG,QAAQ,KAAK,GAAG,IAAI,QAAQ,KAAK,OAAO,CAAC;IAEhE,MAAM,WAAW,GAAG,cAAc;QAChC,CAAC,CAAC,QAAQ;QACV,CAAC,CAAC,mBAAmB,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;IAEjD,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AAC7C,CAAC;AAzCD,qCAyCC"}
1
+ {"version":3,"file":"applyTrailingSlash.js","sourceRoot":"","sources":["../src/applyTrailingSlash.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,+CAAsD;AAQtD,SAAgB,gBAAgB,CAAC,GAAW;IAC1C,OAAO,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;AAC7C,CAAC;AAFD,4CAEC;AAED,qEAAqE;AACrE,SAAwB,kBAAkB,CACxC,IAAY,EACZ,OAAiC;IAEjC,MAAM,EAAC,aAAa,EAAE,OAAO,EAAC,GAAG,OAAO,CAAC;IAEzC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;QACxB,+CAA+C;QAC/C,OAAO,IAAI,CAAC;KACb;IAED,SAAS,mBAAmB,CAAC,GAAW,EAAE,QAAiB;QACzD,OAAO,QAAQ,CAAC,CAAC,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC;IACrE,CAAC;IAED,8CAA8C;IAC9C,IAAI,OAAO,aAAa,KAAK,WAAW,EAAE;QACxC,OAAO,IAAI,CAAC;KACb;IAED,iEAAiE;IACjE,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAA0B,CAAC;IAE/D,4BAA4B;IAC5B,2CAA2C;IAC3C,oEAAoE;IACpE,uEAAuE;IACvE,MAAM,cAAc,GAAG,QAAQ,KAAK,GAAG,IAAI,QAAQ,KAAK,OAAO,CAAC;IAEhE,MAAM,WAAW,GAAG,cAAc;QAChC,CAAC,CAAC,QAAQ;QACV,CAAC,CAAC,mBAAmB,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;IAEjD,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AAC7C,CAAC;AAlCD,qCAkCC;AAED,8DAA8D;AAC9D,SAAgB,eAAe,CAAC,GAAW;IACzC,OAAO,IAAA,uBAAS,EAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAC7B,CAAC;AAFD,0CAEC;AAED,6CAA6C;AAC7C,SAAgB,mBAAmB,CAAC,GAAW;IAC7C,OAAO,IAAA,0BAAY,EAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAChC,CAAC;AAFD,kDAEC"}
package/lib/index.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  export declare const blogPostContainerID = "__blog-post-container";
8
- export { default as applyTrailingSlash, type ApplyTrailingSlashParams, } from './applyTrailingSlash';
8
+ export { default as applyTrailingSlash, addTrailingSlash, addLeadingSlash, removeTrailingSlash, type ApplyTrailingSlashParams, } from './applyTrailingSlash';
9
+ export { addPrefix, removeSuffix, addSuffix, removePrefix } from './stringUtils';
9
10
  export { getErrorCausalChain } from './errorUtils';
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,eAAO,MAAM,mBAAmB,0BAA0B,CAAC;AAE3D,OAAO,EACL,OAAO,IAAI,kBAAkB,EAC7B,KAAK,wBAAwB,GAC9B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,eAAO,MAAM,mBAAmB,0BAA0B,CAAC;AAE3D,OAAO,EACL,OAAO,IAAI,kBAAkB,EAC7B,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,KAAK,wBAAwB,GAC9B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAC,SAAS,EAAE,YAAY,EAAE,SAAS,EAAE,YAAY,EAAC,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC"}
package/lib/index.js CHANGED
@@ -9,12 +9,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  return (mod && mod.__esModule) ? mod : { "default": mod };
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getErrorCausalChain = exports.applyTrailingSlash = exports.blogPostContainerID = void 0;
12
+ exports.getErrorCausalChain = exports.removePrefix = exports.addSuffix = exports.removeSuffix = exports.addPrefix = exports.removeTrailingSlash = exports.addLeadingSlash = exports.addTrailingSlash = exports.applyTrailingSlash = exports.blogPostContainerID = void 0;
13
13
  // __ prefix allows search crawlers (Algolia/DocSearch) to ignore anchors
14
14
  // https://github.com/facebook/docusaurus/issues/8883#issuecomment-1516328368
15
15
  exports.blogPostContainerID = '__blog-post-container';
16
16
  var applyTrailingSlash_1 = require("./applyTrailingSlash");
17
17
  Object.defineProperty(exports, "applyTrailingSlash", { enumerable: true, get: function () { return __importDefault(applyTrailingSlash_1).default; } });
18
+ Object.defineProperty(exports, "addTrailingSlash", { enumerable: true, get: function () { return applyTrailingSlash_1.addTrailingSlash; } });
19
+ Object.defineProperty(exports, "addLeadingSlash", { enumerable: true, get: function () { return applyTrailingSlash_1.addLeadingSlash; } });
20
+ Object.defineProperty(exports, "removeTrailingSlash", { enumerable: true, get: function () { return applyTrailingSlash_1.removeTrailingSlash; } });
21
+ var stringUtils_1 = require("./stringUtils");
22
+ Object.defineProperty(exports, "addPrefix", { enumerable: true, get: function () { return stringUtils_1.addPrefix; } });
23
+ Object.defineProperty(exports, "removeSuffix", { enumerable: true, get: function () { return stringUtils_1.removeSuffix; } });
24
+ Object.defineProperty(exports, "addSuffix", { enumerable: true, get: function () { return stringUtils_1.addSuffix; } });
25
+ Object.defineProperty(exports, "removePrefix", { enumerable: true, get: function () { return stringUtils_1.removePrefix; } });
18
26
  var errorUtils_1 = require("./errorUtils");
19
27
  Object.defineProperty(exports, "getErrorCausalChain", { enumerable: true, get: function () { return errorUtils_1.getErrorCausalChain; } });
20
28
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;AAEH,yEAAyE;AACzE,6EAA6E;AAChE,QAAA,mBAAmB,GAAG,uBAAuB,CAAC;AAE3D,2DAG8B;AAF5B,yIAAA,OAAO,OAAsB;AAG/B,2CAAiD;AAAzC,iHAAA,mBAAmB,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;AAEH,yEAAyE;AACzE,6EAA6E;AAChE,QAAA,mBAAmB,GAAG,uBAAuB,CAAC;AAE3D,2DAM8B;AAL5B,yIAAA,OAAO,OAAsB;AAC7B,sHAAA,gBAAgB,OAAA;AAChB,qHAAA,eAAe,OAAA;AACf,yHAAA,mBAAmB,OAAA;AAGrB,6CAA+E;AAAvE,wGAAA,SAAS,OAAA;AAAE,2GAAA,YAAY,OAAA;AAAE,wGAAA,SAAS,OAAA;AAAE,2GAAA,YAAY,OAAA;AACxD,2CAAiD;AAAzC,iHAAA,mBAAmB,OAAA"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ /** Adds a given string prefix to `str`. */
8
+ export declare function addPrefix(str: string, prefix: string): string;
9
+ /** Removes a given string suffix from `str`. */
10
+ export declare function removeSuffix(str: string, suffix: string): string;
11
+ /** Adds a given string suffix to `str`. */
12
+ export declare function addSuffix(str: string, suffix: string): string;
13
+ /** Removes a given string prefix from `str`. */
14
+ export declare function removePrefix(str: string, prefix: string): string;
15
+ //# sourceMappingURL=stringUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringUtils.d.ts","sourceRoot":"","sources":["../src/stringUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,2CAA2C;AAC3C,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED,gDAAgD;AAChD,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMhE;AAED,2CAA2C;AAC3C,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED,gDAAgD;AAChD,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAEhE"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.removePrefix = exports.addSuffix = exports.removeSuffix = exports.addPrefix = void 0;
10
+ /** Adds a given string prefix to `str`. */
11
+ function addPrefix(str, prefix) {
12
+ return str.startsWith(prefix) ? str : `${prefix}${str}`;
13
+ }
14
+ exports.addPrefix = addPrefix;
15
+ /** Removes a given string suffix from `str`. */
16
+ function removeSuffix(str, suffix) {
17
+ if (suffix === '') {
18
+ // str.slice(0, 0) is ""
19
+ return str;
20
+ }
21
+ return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
22
+ }
23
+ exports.removeSuffix = removeSuffix;
24
+ /** Adds a given string suffix to `str`. */
25
+ function addSuffix(str, suffix) {
26
+ return str.endsWith(suffix) ? str : `${str}${suffix}`;
27
+ }
28
+ exports.addSuffix = addSuffix;
29
+ /** Removes a given string prefix from `str`. */
30
+ function removePrefix(str, prefix) {
31
+ return str.startsWith(prefix) ? str.slice(prefix.length) : str;
32
+ }
33
+ exports.removePrefix = removePrefix;
34
+ //# sourceMappingURL=stringUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringUtils.js","sourceRoot":"","sources":["../src/stringUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,2CAA2C;AAC3C,SAAgB,SAAS,CAAC,GAAW,EAAE,MAAc;IACnD,OAAO,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,GAAG,EAAE,CAAC;AAC1D,CAAC;AAFD,8BAEC;AAED,gDAAgD;AAChD,SAAgB,YAAY,CAAC,GAAW,EAAE,MAAc;IACtD,IAAI,MAAM,KAAK,EAAE,EAAE;QACjB,wBAAwB;QACxB,OAAO,GAAG,CAAC;KACZ;IACD,OAAO,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;AACnE,CAAC;AAND,oCAMC;AAED,2CAA2C;AAC3C,SAAgB,SAAS,CAAC,GAAW,EAAE,MAAc;IACnD,OAAO,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,MAAM,EAAE,CAAC;AACxD,CAAC;AAFD,8BAEC;AAED,gDAAgD;AAChD,SAAgB,YAAY,CAAC,GAAW,EAAE,MAAc;IACtD,OAAO,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;AACjE,CAAC;AAFD,oCAEC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/utils-common",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Common (Node/Browser) utility functions for Docusaurus packages.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -32,5 +32,5 @@
32
32
  "engines": {
33
33
  "node": ">=18.0"
34
34
  },
35
- "gitHead": "a5e675821f0e8b70b591fcebf19fd60a70d55548"
35
+ "gitHead": "5af143651b26b39761361acd96e9c5be7ba0cb25"
36
36
  }
@@ -5,6 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ import {addPrefix, removeSuffix} from './stringUtils';
8
9
  import type {DocusaurusConfig} from '@docusaurus/types';
9
10
 
10
11
  export type ApplyTrailingSlashParams = Pick<
@@ -12,6 +13,10 @@ export type ApplyTrailingSlashParams = Pick<
12
13
  'trailingSlash' | 'baseUrl'
13
14
  >;
14
15
 
16
+ export function addTrailingSlash(str: string): string {
17
+ return str.endsWith('/') ? str : `${str}/`;
18
+ }
19
+
15
20
  // Trailing slash handling depends in some site configuration options
16
21
  export default function applyTrailingSlash(
17
22
  path: string,
@@ -24,13 +29,6 @@ export default function applyTrailingSlash(
24
29
  return path;
25
30
  }
26
31
 
27
- // TODO deduplicate: also present in @docusaurus/utils
28
- function addTrailingSlash(str: string): string {
29
- return str.endsWith('/') ? str : `${str}/`;
30
- }
31
- function removeTrailingSlash(str: string): string {
32
- return str.endsWith('/') ? str.slice(0, -1) : str;
33
- }
34
32
  function handleTrailingSlash(str: string, trailing: boolean): string {
35
33
  return trailing ? addTrailingSlash(str) : removeTrailingSlash(str);
36
34
  }
@@ -55,3 +53,13 @@ export default function applyTrailingSlash(
55
53
 
56
54
  return path.replace(pathname, newPathname);
57
55
  }
56
+
57
+ /** Appends a leading slash to `str`, if one doesn't exist. */
58
+ export function addLeadingSlash(str: string): string {
59
+ return addPrefix(str, '/');
60
+ }
61
+
62
+ /** Removes the trailing slash from `str`. */
63
+ export function removeTrailingSlash(str: string): string {
64
+ return removeSuffix(str, '/');
65
+ }
package/src/index.ts CHANGED
@@ -11,6 +11,10 @@ export const blogPostContainerID = '__blog-post-container';
11
11
 
12
12
  export {
13
13
  default as applyTrailingSlash,
14
+ addTrailingSlash,
15
+ addLeadingSlash,
16
+ removeTrailingSlash,
14
17
  type ApplyTrailingSlashParams,
15
18
  } from './applyTrailingSlash';
19
+ export {addPrefix, removeSuffix, addSuffix, removePrefix} from './stringUtils';
16
20
  export {getErrorCausalChain} from './errorUtils';
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /** Adds a given string prefix to `str`. */
9
+ export function addPrefix(str: string, prefix: string): string {
10
+ return str.startsWith(prefix) ? str : `${prefix}${str}`;
11
+ }
12
+
13
+ /** Removes a given string suffix from `str`. */
14
+ export function removeSuffix(str: string, suffix: string): string {
15
+ if (suffix === '') {
16
+ // str.slice(0, 0) is ""
17
+ return str;
18
+ }
19
+ return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
20
+ }
21
+
22
+ /** Adds a given string suffix to `str`. */
23
+ export function addSuffix(str: string, suffix: string): string {
24
+ return str.endsWith(suffix) ? str : `${str}${suffix}`;
25
+ }
26
+
27
+ /** Removes a given string prefix from `str`. */
28
+ export function removePrefix(str: string, prefix: string): string {
29
+ return str.startsWith(prefix) ? str.slice(prefix.length) : str;
30
+ }