als-require
Advanced tools
Comparing version 0.4.0 to 0.5.0
14
index.js
@@ -7,3 +7,5 @@ const fs = require('fs') | ||
class Require { | ||
static getModule(path, context) { return new Require(path, context).getContent().build() } | ||
static getModule(path, context,contextName) { | ||
return new Require(path, context, contextName).getContent().build() | ||
} | ||
static contents = {} | ||
@@ -17,3 +19,3 @@ static relativePath = calledFrom().replace(rootPath, '') | ||
constructor(path, context = {}) { | ||
constructor(path, context = {},contextName = 'context') { | ||
this.modules = {} | ||
@@ -23,2 +25,3 @@ this.contents = {} | ||
this.context = context | ||
this.contextName = contextName | ||
} | ||
@@ -57,9 +60,10 @@ | ||
build(modules = this.modules) { | ||
build(modules = this.modules,context,contextName = this.contextName) { | ||
function require(path) {return modules[path] || null} | ||
const keys = Object.keys(this.contents).reverse() | ||
if(!context) context = this.context | ||
else context = {...this.context,...context} | ||
keys.map(path => { | ||
const module = { exports: {} } | ||
const params = { module, require, exports: module.exports, context: this.context } | ||
const params = { module, require, exports: module.exports, [contextName]: context } | ||
try { new Function(...Object.keys(params), this.contents[path])(...Object.values(params)) } | ||
@@ -66,0 +70,0 @@ catch (error) {this.error(error,path)} |
{ | ||
"name": "als-require", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -53,5 +53,5 @@ # als-require | ||
class Require { | ||
static getModule(path, context) {} | ||
static getModule(path, context,contextName) {} | ||
static contents = {} | ||
constructor(path, context = {}) { | ||
constructor(path, context = {},contextName) { | ||
this.modules = {} | ||
@@ -63,3 +63,3 @@ this.contents = {} | ||
getContent(path = this.path, from = Require.relativePath) {} | ||
build(modules = this.modules) {} | ||
build(modules = this.modules,context,contextName = this.contextName) {} | ||
} | ||
@@ -73,2 +73,3 @@ ``` | ||
* `context` (Object): shared object which will be available in all modules | ||
* `contextName` (String): Name for context variable (default context) | ||
@@ -85,4 +86,8 @@ | ||
* `require.build()` - builds all modules results | ||
* `require.modules` - includes all module's results | ||
* `require.result` - has the main module result (export) | ||
* parameters: | ||
* `modules` - you can add custom modules (for example modules from backend for same path) | ||
* `context` - private context for curent build (will includes instance.context properties too) | ||
* `contextName` - private conteName for curent build | ||
* `require.modules` - includes all module's results after build | ||
* `require.result` - has the main module result (export) after build | ||
@@ -89,0 +94,0 @@ |
class Require { | ||
static contents = {} | ||
static async getModule(path, context) { | ||
const mod = new Require(path, context) | ||
static async getModule(path, context, contextName) { | ||
const mod = new Require(path, context,contextName) | ||
await mod.getContent() | ||
@@ -24,3 +24,3 @@ return mod.build() | ||
constructor(path, context = {}) { | ||
constructor(path, context = {},contextName = 'context') { | ||
this.modules = {} | ||
@@ -30,2 +30,3 @@ this.contents = {} | ||
this.context = context | ||
this.contextName = contextName | ||
} | ||
@@ -66,9 +67,11 @@ | ||
build(modules = this.modules) { | ||
build(modules = this.modules,context,contextName = this.contextName) { | ||
function require(path) {return modules[path] || null} | ||
const keys = Object.keys(this.contents).reverse() | ||
if(!context) context = this.context | ||
else context = {...this.context,...context} | ||
keys.map(path => { | ||
const module = { exports: {} } | ||
const params = { module, require, exports: module.exports, context: this.context } | ||
const params = { module, require, exports: module.exports, [contextName]: context } | ||
try { new Function(...Object.keys(params), this.contents[path])(...Object.values(params)) } | ||
@@ -75,0 +78,0 @@ catch (error) {this.error(error,path)} |
@@ -10,25 +10,21 @@ const { describe, it } = require('node:test'); | ||
const bn = b ? b.length : 0; | ||
if (an === 0) { | ||
return bn; | ||
} | ||
if (bn === 0) { | ||
return an; | ||
} | ||
if (an === 0) return bn; | ||
if (bn === 0) return an; | ||
const matrix = new Array(bn + 1); | ||
for (let i = 0; i <= bn; ++i) { | ||
matrix[i] = new Array(an + 1); | ||
matrix[i][0] = i; | ||
matrix[i] = new Array(an + 1); | ||
matrix[i][0] = i; | ||
} | ||
for (let j = 0; j <= an; ++j) { | ||
matrix[0][j] = j; | ||
matrix[0][j] = j; | ||
} | ||
for (let i = 1; i <= bn; ++i) { | ||
for (let j = 1; j <= an; ++j) { | ||
const cost = (b[i - 1] === a[j - 1]) ? 0 : 1; | ||
matrix[i][j] = Math.min( | ||
matrix[i - 1][j] + 1, // Deletion | ||
matrix[i][j - 1] + 1, // Insertion | ||
matrix[i - 1][j - 1] + cost // Substitution | ||
); | ||
} | ||
for (let j = 1; j <= an; ++j) { | ||
const cost = (b[i - 1] === a[j - 1]) ? 0 : 1; | ||
matrix[i][j] = Math.min( | ||
matrix[i - 1][j] + 1, // Deletion | ||
matrix[i][j - 1] + 1, // Insertion | ||
matrix[i - 1][j - 1] + cost // Substitution | ||
); | ||
} | ||
} | ||
@@ -70,6 +66,6 @@ return matrix[bn][an]; | ||
Object.entries(mod.contents).forEach(([path,content]) => { | ||
const fullPath = join(__dirname,'..',path) | ||
const actual = fs.readFileSync(fullPath,'utf-8') | ||
assert(areStringsApproximatelyEqual(actual,content)) | ||
Object.entries(mod.contents).forEach(([path, content]) => { | ||
const fullPath = join(__dirname, '..', path) | ||
const actual = fs.readFileSync(fullPath, 'utf-8') | ||
assert(areStringsApproximatelyEqual(actual, content)) | ||
}) | ||
@@ -109,3 +105,3 @@ | ||
mod.build() | ||
assert(false,'Should throw error') | ||
assert(false, 'Should throw error') | ||
} catch (error) { | ||
@@ -127,3 +123,3 @@ // console.log(error) | ||
const result = mod.result | ||
assert(result.calledFrom === null) | ||
@@ -135,1 +131,38 @@ assert(typeof result.calledFrom1 === 'function') | ||
}); | ||
describe('Require class extended tests', () => { | ||
it('should allow changing the context name dynamically', () => { | ||
const req = new Require('./modules/testModule', { customKey: 'initial value' }); | ||
const context = { customKey: 'customValue' }; | ||
req.getContent(); | ||
const { result } = req.build(undefined, context, 'customContext'); | ||
assert.deepStrictEqual(result, { result: 'Success', contextValue: 'customValue' }) | ||
}); | ||
it('should handle multiple contexts correctly', () => { | ||
const req = new Require('./modules/testModule'); | ||
req.getContent(); | ||
// Построение с первым контекстом | ||
const firstContext = { data: 'first' }; | ||
const { result:firstResult } = req.build({}, firstContext, 'firstContext'); | ||
assert.deepStrictEqual(firstResult, { result: 'Success', contextValue: 'first' }) | ||
const secondContext = { data: 'second' }; | ||
const { result:secondResult } = req.build({}, secondContext, 'secondContext'); | ||
assert.deepStrictEqual(secondResult, { result: 'Success', contextValue: 'second' }) | ||
}); | ||
it('should correctly handle custom context names in errors', () => { | ||
const req = new Require('./modules/moduleWithError'); | ||
req.getContent(); | ||
try { | ||
// Используем кастомное имя контекста, которое будет упомянуто в ошибке | ||
req.build({}, { errorDetail: 'critical' }, 'customErrorContext'); | ||
assert.fail('Should have thrown an error due to module error'); | ||
} catch (error) { | ||
assert(error.message.includes('customErrorContext'), 'Error does not correctly reference the custom context name'); | ||
} | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
27739
17
365
158
3