@docusaurus/utils
Advanced tools
Comparing version 2.0.0-alpha.61 to 2.0.0-alpha.62
@@ -73,3 +73,7 @@ /** | ||
export declare function getFilePathForRoutePath(routePath: string): string; | ||
export declare function getElementsAround<T extends unknown>(array: T[], aroundIndex: number): { | ||
next: T | undefined; | ||
previous: T | undefined; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFilePathForRoutePath = exports.removePrefix = exports.removeSuffix = exports.removeTrailingSlash = exports.addTrailingSlash = exports.addLeadingSlash = exports.resolvePathname = exports.isValidPathname = exports.getEditUrl = exports.aliasedSitePath = exports.normalizeUrl = exports.parseMarkdownFile = exports.parseMarkdownString = exports.createExcerpt = exports.getSubFolder = exports.idx = exports.genChunkName = exports.posixPath = exports.genComponentName = exports.upperFirst = exports.docuHash = exports.simpleHash = exports.encodePath = exports.fileToPath = exports.objectWithKeySorted = exports.generate = void 0; | ||
exports.getElementsAround = exports.getFilePathForRoutePath = exports.removePrefix = exports.removeSuffix = exports.removeTrailingSlash = exports.addTrailingSlash = exports.addLeadingSlash = exports.resolvePathname = exports.isValidPathname = exports.getEditUrl = exports.aliasedSitePath = exports.normalizeUrl = exports.parseMarkdownFile = exports.parseMarkdownString = exports.createExcerpt = exports.getSubFolder = exports.idx = exports.genChunkName = exports.posixPath = exports.genComponentName = exports.upperFirst = exports.docuHash = exports.simpleHash = exports.encodePath = exports.fileToPath = exports.objectWithKeySorted = exports.generate = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
@@ -314,3 +314,4 @@ const gray_matter_1 = __importDefault(require("gray-matter")); | ||
// weird, but is there a better way? | ||
return new url_1.URL(str, 'https://domain.com').pathname === str; | ||
const parsedPathname = new url_1.URL(str, 'https://domain.com').pathname; | ||
return parsedPathname === str || parsedPathname === encodeURI(str); | ||
} | ||
@@ -356,1 +357,12 @@ catch (e) { | ||
exports.getFilePathForRoutePath = getFilePathForRoutePath; | ||
function getElementsAround(array, aroundIndex) { | ||
const min = 0; | ||
const max = array.length - 1; | ||
if (aroundIndex < min || aroundIndex > max) { | ||
throw new Error(`Valid aroundIndex for array (of size ${array.length}) are between ${min} and ${max}, but you provided aroundIndex=${aroundIndex}`); | ||
} | ||
const previous = aroundIndex === min ? undefined : array[aroundIndex - 1]; | ||
const next = aroundIndex === max ? undefined : array[aroundIndex + 1]; | ||
return { previous, next }; | ||
} | ||
exports.getElementsAround = getElementsAround; |
{ | ||
"name": "@docusaurus/utils", | ||
"version": "2.0.0-alpha.61", | ||
"version": "2.0.0-alpha.62", | ||
"description": "Node utility functions for Docusaurus packages", | ||
@@ -26,3 +26,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "0b5432e9dfed6fc46fd181a9be5f0b33251e17f4" | ||
"gitHead": "d66d1a238687aa47bf3951ecd50cfa0940f221b3" | ||
} |
@@ -29,2 +29,3 @@ /** | ||
addLeadingSlash, | ||
getElementsAround, | ||
} from '../index'; | ||
@@ -398,6 +399,7 @@ | ||
expect(isValidPathname('/hey///ho///')).toBe(true); // Unexpected but valid | ||
expect(isValidPathname('/hey/héllô you')).toBe(true); | ||
// | ||
expect(isValidPathname('')).toBe(false); | ||
expect(isValidPathname('hey')).toBe(false); | ||
expect(isValidPathname('/hey/hô')).toBe(false); | ||
expect(isValidPathname('/hey?qs=ho')).toBe(false); | ||
@@ -481,1 +483,35 @@ expect(isValidPathname('https://fb.com/hey')).toBe(false); | ||
}); | ||
describe('getElementsAround', () => { | ||
test('can return elements around', () => { | ||
expect(getElementsAround(['a', 'b', 'c', 'd'], 0)).toEqual({ | ||
previous: undefined, | ||
next: 'b', | ||
}); | ||
expect(getElementsAround(['a', 'b', 'c', 'd'], 1)).toEqual({ | ||
previous: 'a', | ||
next: 'c', | ||
}); | ||
expect(getElementsAround(['a', 'b', 'c', 'd'], 2)).toEqual({ | ||
previous: 'b', | ||
next: 'd', | ||
}); | ||
expect(getElementsAround(['a', 'b', 'c', 'd'], 3)).toEqual({ | ||
previous: 'c', | ||
next: undefined, | ||
}); | ||
}); | ||
test('throws if bad index is provided', () => { | ||
expect(() => | ||
getElementsAround(['a', 'b', 'c', 'd'], -1), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Valid aroundIndex for array (of size 4) are between 0 and 3, but you provided aroundIndex=-1"`, | ||
); | ||
expect(() => | ||
getElementsAround(['a', 'b', 'c', 'd'], 4), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Valid aroundIndex for array (of size 4) are between 0 and 3, but you provided aroundIndex=4"`, | ||
); | ||
}); | ||
}); |
@@ -368,3 +368,4 @@ /** | ||
// weird, but is there a better way? | ||
return new URL(str, 'https://domain.com').pathname === str; | ||
const parsedPathname = new URL(str, 'https://domain.com').pathname; | ||
return parsedPathname === str || parsedPathname === encodeURI(str); | ||
} catch (e) { | ||
@@ -406,1 +407,20 @@ return false; | ||
} | ||
export function getElementsAround<T extends unknown>( | ||
array: T[], | ||
aroundIndex: number, | ||
): { | ||
next: T | undefined; | ||
previous: T | undefined; | ||
} { | ||
const min = 0; | ||
const max = array.length - 1; | ||
if (aroundIndex < min || aroundIndex > max) { | ||
throw new Error( | ||
`Valid aroundIndex for array (of size ${array.length}) are between ${min} and ${max}, but you provided aroundIndex=${aroundIndex}`, | ||
); | ||
} | ||
const previous = aroundIndex === min ? undefined : array[aroundIndex - 1]; | ||
const next = aroundIndex === max ? undefined : array[aroundIndex + 1]; | ||
return {previous, next}; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
496676
1300