@lwrjs/shared-utils
Advanced tools
Comparing version 0.0.2-alpha14 to 0.0.2-alpha15
@@ -16,2 +16,3 @@ "use strict"; | ||
__exportStar(require("./html-meta"), exports); | ||
__exportStar(require("./identity"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -6,29 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getViewSourceFromFile = exports.replaceStringFromLocation = exports.moduleSpecifierToKebabCase = exports.kebabcaseToCamelcase = exports.slugify = exports.debounce = exports.deepFreeze = exports.resolveFileExtension = exports.serializeModuleToJson = exports.isVirtualModuleEntry = exports.readFile = exports.hashContent = exports.normalizeVersionFromUri = exports.normalizeVersionToUri = exports.getVersionedSpecifier = exports.explodeMode = exports.explodeName = exports.explodeSpecifier = exports.getSpecifier = exports.DEFAULT_NAMESPACE = void 0; | ||
exports.getViewSourceFromFile = exports.replaceStringFromLocation = exports.debounce = exports.deepFreeze = exports.resolveFileExtension = exports.serializeModuleToJson = exports.readFile = exports.hashContent = exports.explodeMode = void 0; | ||
const fs_1 = __importDefault(require("fs")); | ||
const path_1 = __importDefault(require("path")); | ||
const crypto_1 = __importDefault(require("crypto")); | ||
const slugify_1 = __importDefault(require("slugify")); | ||
exports.DEFAULT_NAMESPACE = 'lwr-lib'; // it must have a dash so its guarantee to not collide | ||
function getSpecifier({ namespace, name }) { | ||
return `${namespace}/${name}`; | ||
} | ||
exports.getSpecifier = getSpecifier; | ||
function explodeSpecifier(specifier, defaultNs) { | ||
const [namespace, name, ...remaining] = specifier.split('/'); | ||
return name | ||
? [namespace, [name, ...remaining].join('/')] | ||
: [defaultNs ? defaultNs : exports.DEFAULT_NAMESPACE, namespace]; | ||
} | ||
exports.explodeSpecifier = explodeSpecifier; | ||
function explodeName(rawName) { | ||
const isRelativeResource = rawName.includes('/'); // ex. ns/name/name.html | ||
if (!isRelativeResource) { | ||
return [rawName, undefined]; | ||
} | ||
const nameParts = rawName.split('/'); | ||
const [name, ...relativeParts] = nameParts; | ||
return [name, relativeParts.join('/')]; | ||
} | ||
exports.explodeName = explodeName; | ||
const identity_1 = require("./identity"); | ||
function explodeMode(mode) { | ||
@@ -51,34 +29,2 @@ switch (mode) { | ||
exports.explodeMode = explodeMode; | ||
/** | ||
* Create a versioned specifier based on the module registry | ||
* | ||
* @remarks | ||
* If the incoming specifier is already versioned, it will be returned as is. | ||
* | ||
* @param specifier - the module specifier | ||
* @param moduleRegistry - the public module registry | ||
* | ||
* @returns the specifier for the latest version | ||
* | ||
* @example - 'c/form' => 'c/form/v/0_0_1' | ||
* @example - 'c/form/v/0_0_1' => 'c/form/v/0_0_1' | ||
*/ | ||
async function getVersionedSpecifier(specifier, moduleRegistry) { | ||
if (specifier.includes('/v/')) { | ||
return specifier; | ||
} | ||
const moduleEntry = await moduleRegistry.getModuleEntry({ specifier }); | ||
const [namespace, name] = explodeSpecifier(specifier); | ||
const encodedSpecifier = getSpecifier({ namespace, name: encodeURIComponent(name) }); | ||
return moduleEntry ? `${encodedSpecifier}/v/${moduleEntry.version}` : encodedSpecifier; | ||
} | ||
exports.getVersionedSpecifier = getVersionedSpecifier; | ||
function normalizeVersionToUri(version) { | ||
return version.replace(/\./g, '_'); | ||
} | ||
exports.normalizeVersionToUri = normalizeVersionToUri; | ||
function normalizeVersionFromUri(version) { | ||
return version.replace(/_/g, '.'); | ||
} | ||
exports.normalizeVersionFromUri = normalizeVersionFromUri; | ||
function hashContent(source) { | ||
@@ -92,13 +38,7 @@ return crypto_1.default.createHash('md5').update(source).digest('hex'); | ||
exports.readFile = readFile; | ||
function isVirtualModuleEntry(moduleEntry) { | ||
return !!moduleEntry.virtual; | ||
} | ||
exports.isVirtualModuleEntry = isVirtualModuleEntry; | ||
// Given a Module Identifier, return a JSON entry | ||
async function createJsonModule(moduleId, moduleRegistry, context) { | ||
const specifier = getSpecifier({ namespace: moduleId.namespace, name: moduleId.name }); | ||
const { version } = await moduleRegistry.getModuleEntry({ specifier }); | ||
const { ownHash } = await moduleRegistry.getModuleSource(moduleId, context); | ||
const { ownHash, moduleEntry: { version }, } = await moduleRegistry.getModuleSource(moduleId, context); | ||
return { | ||
specifier, | ||
specifier: moduleId.specifier, | ||
version, | ||
@@ -120,12 +60,7 @@ ownHash, | ||
*/ | ||
async function serializeModuleToJson(code = '', { name, namespace, version, ownHash, moduleDefinition: { format, moduleRecord: { imports = [] }, }, }, moduleRegistry, context) { | ||
async function serializeModuleToJson(code = '', { specifier, version, ownHash, moduleDefinition: { format, moduleRecord: { imports = [] }, }, }, moduleRegistry, context) { | ||
// Build the dependencies array | ||
const dependencies = imports.map(({ namespace, name, version }) => createJsonModule({ | ||
namespace, | ||
name, | ||
version, | ||
resourceType: 'module', | ||
}, moduleRegistry, context)); | ||
const dependencies = imports.map((dep) => createJsonModule(dep, moduleRegistry, context)); | ||
return { | ||
specifier: getSpecifier({ namespace, name }), | ||
specifier, | ||
version, | ||
@@ -213,55 +148,2 @@ ownHash, | ||
/** | ||
* Turn a string into a slug | ||
* | ||
* @example - 'This IS a sentence' => 'this-is-a-sentence' | ||
* @param name a string to slugify | ||
*/ | ||
function slugify(name) { | ||
return slugify_1.default(name, { | ||
lower: true, | ||
}); | ||
} | ||
exports.slugify = slugify; | ||
/** | ||
* Turn a string into kebab case to camel case | ||
* | ||
* @example - 'name-of-something' => 'name/ofSomething' | ||
* @param name A string in kebab case | ||
*/ | ||
function kebabcaseToCamelcase(name) { | ||
const newName = []; | ||
let nsFound = false; | ||
let upper = false; | ||
for (const currChar of name) { | ||
if (currChar === '-') { | ||
if (!nsFound) { | ||
nsFound = true; | ||
newName.push('/'); | ||
} | ||
else { | ||
upper = true; | ||
} | ||
} | ||
else { | ||
newName.push(upper ? currChar.toUpperCase() : currChar); | ||
upper = false; | ||
} | ||
} | ||
return newName.join(''); | ||
} | ||
exports.kebabcaseToCamelcase = kebabcaseToCamelcase; | ||
/** | ||
* Clone of lwr/init, reverse of kebabcaseToCamelcase, strips off versions | ||
* | ||
* @example - 'name/ofSomething/v/1.0.0' => 'name-of-something' | ||
* @param specifier | ||
*/ | ||
function moduleSpecifierToKebabCase(specifier) { | ||
return specifier | ||
.replace(/\/v\/[a-zA-Z0-9-_.]+$/, '') | ||
.replace('/', '-') | ||
.replace(/([A-Z])/g, (c) => `-${c.toLowerCase()}`); | ||
} | ||
exports.moduleSpecifierToKebabCase = moduleSpecifierToKebabCase; | ||
/** | ||
* Replace a part of a source string at the indices with a different value | ||
@@ -290,3 +172,3 @@ * | ||
name, | ||
slug: slugify(name), | ||
slug: identity_1.slugify(name), | ||
filePath: sourceFilePath, | ||
@@ -293,0 +175,0 @@ ownHash: hashContent(viewSource), |
export * from './utils'; | ||
export * from './observable'; | ||
export * from './html-meta'; | ||
export * from './identity'; | ||
//# sourceMappingURL=index.js.map |
import fs from 'fs'; | ||
import path from 'path'; | ||
import crypto from 'crypto'; | ||
import slugifyText from 'slugify'; | ||
export const DEFAULT_NAMESPACE = 'lwr-lib'; // it must have a dash so its guarantee to not collide | ||
export function getSpecifier({ namespace, name }) { | ||
return `${namespace}/${name}`; | ||
} | ||
export function explodeSpecifier(specifier, defaultNs) { | ||
const [namespace, name, ...remaining] = specifier.split('/'); | ||
return name | ||
? [namespace, [name, ...remaining].join('/')] | ||
: [defaultNs ? defaultNs : DEFAULT_NAMESPACE, namespace]; | ||
} | ||
export function explodeName(rawName) { | ||
const isRelativeResource = rawName.includes('/'); // ex. ns/name/name.html | ||
if (!isRelativeResource) { | ||
return [rawName, undefined]; | ||
} | ||
const nameParts = rawName.split('/'); | ||
const [name, ...relativeParts] = nameParts; | ||
return [name, relativeParts.join('/')]; | ||
} | ||
import { slugify } from './identity'; | ||
export function explodeMode(mode) { | ||
@@ -40,31 +21,2 @@ switch (mode) { | ||
} | ||
/** | ||
* Create a versioned specifier based on the module registry | ||
* | ||
* @remarks | ||
* If the incoming specifier is already versioned, it will be returned as is. | ||
* | ||
* @param specifier - the module specifier | ||
* @param moduleRegistry - the public module registry | ||
* | ||
* @returns the specifier for the latest version | ||
* | ||
* @example - 'c/form' => 'c/form/v/0_0_1' | ||
* @example - 'c/form/v/0_0_1' => 'c/form/v/0_0_1' | ||
*/ | ||
export async function getVersionedSpecifier(specifier, moduleRegistry) { | ||
if (specifier.includes('/v/')) { | ||
return specifier; | ||
} | ||
const moduleEntry = await moduleRegistry.getModuleEntry({ specifier }); | ||
const [namespace, name] = explodeSpecifier(specifier); | ||
const encodedSpecifier = getSpecifier({ namespace, name: encodeURIComponent(name) }); | ||
return moduleEntry ? `${encodedSpecifier}/v/${moduleEntry.version}` : encodedSpecifier; | ||
} | ||
export function normalizeVersionToUri(version) { | ||
return version.replace(/\./g, '_'); | ||
} | ||
export function normalizeVersionFromUri(version) { | ||
return version.replace(/_/g, '.'); | ||
} | ||
export function hashContent(source) { | ||
@@ -76,12 +28,7 @@ return crypto.createHash('md5').update(source).digest('hex'); | ||
} | ||
export function isVirtualModuleEntry(moduleEntry) { | ||
return !!moduleEntry.virtual; | ||
} | ||
// Given a Module Identifier, return a JSON entry | ||
async function createJsonModule(moduleId, moduleRegistry, context) { | ||
const specifier = getSpecifier({ namespace: moduleId.namespace, name: moduleId.name }); | ||
const { version } = await moduleRegistry.getModuleEntry({ specifier }); | ||
const { ownHash } = await moduleRegistry.getModuleSource(moduleId, context); | ||
const { ownHash, moduleEntry: { version }, } = await moduleRegistry.getModuleSource(moduleId, context); | ||
return { | ||
specifier, | ||
specifier: moduleId.specifier, | ||
version, | ||
@@ -103,12 +50,7 @@ ownHash, | ||
*/ | ||
export async function serializeModuleToJson(code = '', { name, namespace, version, ownHash, moduleDefinition: { format, moduleRecord: { imports = [] }, }, }, moduleRegistry, context) { | ||
export async function serializeModuleToJson(code = '', { specifier, version, ownHash, moduleDefinition: { format, moduleRecord: { imports = [] }, }, }, moduleRegistry, context) { | ||
// Build the dependencies array | ||
const dependencies = imports.map(({ namespace, name, version }) => createJsonModule({ | ||
namespace, | ||
name, | ||
version, | ||
resourceType: 'module', | ||
}, moduleRegistry, context)); | ||
const dependencies = imports.map((dep) => createJsonModule(dep, moduleRegistry, context)); | ||
return { | ||
specifier: getSpecifier({ namespace, name }), | ||
specifier, | ||
version, | ||
@@ -193,52 +135,2 @@ ownHash, | ||
/** | ||
* Turn a string into a slug | ||
* | ||
* @example - 'This IS a sentence' => 'this-is-a-sentence' | ||
* @param name a string to slugify | ||
*/ | ||
export function slugify(name) { | ||
return slugifyText(name, { | ||
lower: true, | ||
}); | ||
} | ||
/** | ||
* Turn a string into kebab case to camel case | ||
* | ||
* @example - 'name-of-something' => 'name/ofSomething' | ||
* @param name A string in kebab case | ||
*/ | ||
export function kebabcaseToCamelcase(name) { | ||
const newName = []; | ||
let nsFound = false; | ||
let upper = false; | ||
for (const currChar of name) { | ||
if (currChar === '-') { | ||
if (!nsFound) { | ||
nsFound = true; | ||
newName.push('/'); | ||
} | ||
else { | ||
upper = true; | ||
} | ||
} | ||
else { | ||
newName.push(upper ? currChar.toUpperCase() : currChar); | ||
upper = false; | ||
} | ||
} | ||
return newName.join(''); | ||
} | ||
/** | ||
* Clone of lwr/init, reverse of kebabcaseToCamelcase, strips off versions | ||
* | ||
* @example - 'name/ofSomething/v/1.0.0' => 'name-of-something' | ||
* @param specifier | ||
*/ | ||
export function moduleSpecifierToKebabCase(specifier) { | ||
return specifier | ||
.replace(/\/v\/[a-zA-Z0-9-_.]+$/, '') | ||
.replace('/', '-') | ||
.replace(/([A-Z])/g, (c) => `-${c.toLowerCase()}`); | ||
} | ||
/** | ||
* Replace a part of a source string at the indices with a different value | ||
@@ -245,0 +137,0 @@ * |
export * from './utils'; | ||
export * from './observable'; | ||
export * from './html-meta'; | ||
export * from './identity'; | ||
//# sourceMappingURL=index.d.ts.map |
/// <reference types="node" /> | ||
import { ApplicationMode, ModeConfig, ModuleDefinition, ModuleEntry, ModuleJsonDefinition, ModuleRegistry, RuntimeContext, ViewSource, VirtualModuleEntry, PublicModuleRegistry } from '@lwrjs/types'; | ||
interface SpecifierConfig { | ||
name: string; | ||
namespace: string; | ||
} | ||
export declare const DEFAULT_NAMESPACE = "lwr-lib"; | ||
export declare function getSpecifier({ namespace, name }: SpecifierConfig): string; | ||
export declare function explodeSpecifier(specifier: string, defaultNs?: string): [string, string]; | ||
export declare function explodeName(rawName: string): [string, string | undefined]; | ||
import { ApplicationMode, ModeConfig, ModuleDefinition, ModuleJsonDefinition, ModuleRegistry, RuntimeContext, ViewSource } from '@lwrjs/types'; | ||
export declare function explodeMode(mode: ApplicationMode): ModeConfig; | ||
/** | ||
* Create a versioned specifier based on the module registry | ||
* | ||
* @remarks | ||
* If the incoming specifier is already versioned, it will be returned as is. | ||
* | ||
* @param specifier - the module specifier | ||
* @param moduleRegistry - the public module registry | ||
* | ||
* @returns the specifier for the latest version | ||
* | ||
* @example - 'c/form' => 'c/form/v/0_0_1' | ||
* @example - 'c/form/v/0_0_1' => 'c/form/v/0_0_1' | ||
*/ | ||
export declare function getVersionedSpecifier(specifier: string, moduleRegistry: PublicModuleRegistry): Promise<string>; | ||
export declare function normalizeVersionToUri(version: string): string; | ||
export declare function normalizeVersionFromUri(version: string): string; | ||
export declare function hashContent(source: string | Buffer): string; | ||
export declare function readFile(filePath: string): string; | ||
export declare function isVirtualModuleEntry(moduleEntry: ModuleEntry): moduleEntry is VirtualModuleEntry; | ||
/** | ||
@@ -41,3 +15,3 @@ * Take a Module Definition and return its JSON serialization | ||
*/ | ||
export declare function serializeModuleToJson(code: string | undefined, { name, namespace, version, ownHash, moduleDefinition: { format, moduleRecord: { imports }, }, }: ModuleDefinition, moduleRegistry: ModuleRegistry, context: RuntimeContext): Promise<ModuleJsonDefinition>; | ||
export declare function serializeModuleToJson(code: string | undefined, { specifier, version, ownHash, moduleDefinition: { format, moduleRecord: { imports }, }, }: ModuleDefinition, moduleRegistry: ModuleRegistry, context: RuntimeContext): Promise<ModuleJsonDefinition>; | ||
export declare function resolveFileExtension(filePath: string): string; | ||
@@ -59,23 +33,2 @@ /** | ||
/** | ||
* Turn a string into a slug | ||
* | ||
* @example - 'This IS a sentence' => 'this-is-a-sentence' | ||
* @param name a string to slugify | ||
*/ | ||
export declare function slugify(name: string): string; | ||
/** | ||
* Turn a string into kebab case to camel case | ||
* | ||
* @example - 'name-of-something' => 'name/ofSomething' | ||
* @param name A string in kebab case | ||
*/ | ||
export declare function kebabcaseToCamelcase(name: string): string; | ||
/** | ||
* Clone of lwr/init, reverse of kebabcaseToCamelcase, strips off versions | ||
* | ||
* @example - 'name/ofSomething/v/1.0.0' => 'name-of-something' | ||
* @param specifier | ||
*/ | ||
export declare function moduleSpecifierToKebabCase(specifier: string): string; | ||
/** | ||
* Replace a part of a source string at the indices with a different value | ||
@@ -98,3 +51,2 @@ * | ||
export declare function getViewSourceFromFile(source: string, viewFolder: string): ViewSource; | ||
export {}; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.0.2-alpha14", | ||
"version": "0.0.2-alpha15", | ||
"homepage": "https://lwr.dev/", | ||
@@ -43,3 +43,3 @@ "repository": { | ||
}, | ||
"gitHead": "1c76575ab5ee559f57860a17763fff6874d21e5a" | ||
"gitHead": "bfa9d414185ef452a28474aefe73910521071d99" | ||
} |
42228
18
1020