@mstream/di
Advanced tools
Comparing version 2.0.4 to 2.0.12
@@ -5,2 +5,18 @@ # Changelog | ||
### 2.0.12 (2023-01-23) | ||
### 2.0.11 (2023-01-23) | ||
### 2.0.10 (2023-01-23) | ||
### 2.0.9 (2023-01-23) | ||
### 2.0.8 (2023-01-23) | ||
### 2.0.7 (2023-01-23) | ||
### 2.0.6 (2023-01-23) | ||
### 2.0.5 (2023-01-23) | ||
### 2.0.4 (2023-01-19) | ||
@@ -7,0 +23,0 @@ |
@@ -1,9 +0,10 @@ | ||
declare class ContextBuilder { | ||
export declare class ContextBuilder { | ||
private cache; | ||
private context; | ||
private wasBuilt; | ||
build(): object; | ||
constructor(); | ||
build(options?: { | ||
eagerly: boolean; | ||
}): object; | ||
register(name: string, creator: (context: object) => any): this; | ||
} | ||
export declare function contextBuilder(): ContextBuilder; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
class ContextBuilder { | ||
export class ContextBuilder { | ||
constructor() { | ||
@@ -7,4 +7,7 @@ this.cache = {}; | ||
} | ||
build() { | ||
build(options = { eagerly: false }) { | ||
this.wasBuilt = true; | ||
if (options.eagerly) { | ||
Object.getOwnPropertyNames(this.context).forEach((name) => this.context[name]); | ||
} | ||
return this.context; | ||
@@ -34,4 +37,1 @@ } | ||
} | ||
export function contextBuilder() { | ||
return new ContextBuilder(); | ||
} |
@@ -9,10 +9,12 @@ { | ||
"format:js": "prettier --write src/js", | ||
"format": "npm run format:js", | ||
"generate:howto": "markdown-inject how-to/README.md && prettier --write how-to", | ||
"generate:reference": "typedoc --disableSources --out reference --readme none ../src/ts/di.ts && prettier --write reference", | ||
"generate:tutorial": "markdown-inject tutorial/README.md && prettier --write tutorial", | ||
"generate": "npm run format:js && npm run generate:howto && npm run generate:reference && npm run generate:tutorial", | ||
"format": "prettier --write .", | ||
"generate:howto": "markdown-inject src/md/how-to/README.md && prettier --write src/md/how-to", | ||
"generate:reference": "typedoc --disableSources --excludePrivate --out src/md/reference --readme none ../src/ts/di.ts && ./fix-links.sh && prettier --write src/md/reference", | ||
"generate:tutorial": "markdown-inject src/md/tutorial/README.md && prettier --write src/md/tutorial", | ||
"generate": "npm run format && npm run generate:howto && npm run generate:reference && npm run generate:tutorial", | ||
"lint:js": "eslint src/js", | ||
"lint:md": "find . -name '*.md' -not -path './node_modules/*' -exec markdown-link-check {} \\;", | ||
"lint": "prettier --check . && npm run lint:js && npm run lint:md" | ||
"lint:md": "find . -name '*.md' -not -name '_sidebar.md' -not -path './node_modules/*' -not -path './src/md/reference/*' -not -path './dist/*' -exec markdown-link-check {} \\;", | ||
"lint": "prettier --check . && npm run lint:js && npm run lint:md", | ||
"publish": "rm -rf dist && mkdir dist && cp index.html dist/ && cp _sidebar.md dist/ && cp --parents -R src/md dist/ && gh-pages -d dist -u 'ci <ci@github.com>'", | ||
"start": "docsify serve ." | ||
}, | ||
@@ -25,3 +27,5 @@ "author": "Maciej Laciak <maciej.laciak@gmail.com> (https://github.com/mstream/)", | ||
"devDependencies": { | ||
"docsify-cli": "^4", | ||
"eslint": "^8", | ||
"gh-pages": "^5", | ||
"markdown-inject": "^3", | ||
@@ -28,0 +32,0 @@ "markdown-link-check": "^3", |
@@ -1,6 +0,6 @@ | ||
import { contextBuilder } from "@mstream/di" | ||
import { ContextBuilder } from "@mstream/di" | ||
const englishGreeting = "Hello" | ||
contextBuilder() | ||
new ContextBuilder() | ||
.register("greeting", () => englishGreeting) | ||
@@ -7,0 +7,0 @@ .register( |
@@ -9,2 +9,4 @@ { | ||
"scripts": { | ||
"format:js": "prettier --write src/js test/js", | ||
"format": "npm run format:js", | ||
"lint:js": "eslint --config ../.eslintrc.json .", | ||
@@ -25,2 +27,3 @@ "lint:md": "markdown-link-check *.md", | ||
"markdown-link-check": "^3", | ||
"prettier": "^2", | ||
"sinon": "^15", | ||
@@ -27,0 +30,0 @@ "vitest": "~0.27" |
@@ -1,6 +0,6 @@ | ||
import { contextBuilder } from "@mstream/di" | ||
import { ContextBuilder } from "@mstream/di" | ||
import appCreator from "./app.js" | ||
import loggerCreator, { WARNING } from "./logger.js" | ||
contextBuilder() | ||
new ContextBuilder() | ||
.register(`console`, () => console) | ||
@@ -7,0 +7,0 @@ .register(`config`, () => ({ logLevel: WARNING })) |
{ | ||
"name": "@mstream/di", | ||
"version": "2.0.4", | ||
"version": "2.0.12", | ||
"description": "", | ||
@@ -10,6 +10,7 @@ "private": false, | ||
"scripts": { | ||
"build": "tsc", | ||
"build:ts": "tsc", | ||
"build": "npm run build:ts", | ||
"format:docs": "cd docs && npm run format", | ||
"format:examples": "cd examples && npm run format", | ||
"format": "prettier --write . && npm run format:docs && npm run format:examples", | ||
"format:example": "cd example && npm run format", | ||
"format": "prettier --write . && npm run format:docs && npm run format:example", | ||
"generate:docs": "cd docs && npm run generate", | ||
@@ -23,2 +24,4 @@ "generate": "npm run generate:docs", | ||
"lint": "prettier --check . && npm run lint:commits && npm run lint:docs && npm run lint:js && npm run lint:md && npm run lint:example", | ||
"publish:docs": "cd docs && npm run publish", | ||
"publish:package": "npm publish --access public", | ||
"release:git": "standard-version", | ||
@@ -29,2 +32,3 @@ "release:gh": "conventional-github-releaser", | ||
"test": "npm run test:js && npm run test:example", | ||
"start:docs": "cd docs && npm start", | ||
"start:example": "cd example && npm start" | ||
@@ -31,0 +35,0 @@ }, |
@@ -7,5 +7,5 @@ # DI | ||
- [how-to guide](docs/how-to/README.md) | ||
- [reference](docs/reference/README.md) | ||
- [tutorial](docs/tutorial/README.md) | ||
- [how-to guide](docs/src/md/how-to/README.md) | ||
- [reference](docs/src/md/reference/README.md) | ||
- [tutorial](docs/src/md/tutorial/README.md) | ||
@@ -12,0 +12,0 @@ ## Usage Examples |
@@ -1,2 +0,2 @@ | ||
class ContextBuilder { | ||
export class ContextBuilder { | ||
private cache = {} | ||
@@ -6,8 +6,20 @@ private context = {} | ||
build(): object { | ||
public constructor() {} | ||
public build( | ||
options: { eagerly: boolean } = { eagerly: false }, | ||
): object { | ||
this.wasBuilt = true | ||
if (options.eagerly) { | ||
Object.getOwnPropertyNames(this.context).forEach( | ||
(name) => this.context[name], | ||
) | ||
} | ||
return this.context | ||
} | ||
register(name: string, creator: (context: object) => any): this { | ||
public register( | ||
name: string, | ||
creator: (context: object) => any, | ||
): this { | ||
if (this.wasBuilt) { | ||
@@ -40,5 +52,1 @@ throw Error(`The context has already been built.`) | ||
} | ||
export function contextBuilder() { | ||
return new ContextBuilder() | ||
} |
import { describe, expect, it } from "vitest" | ||
import { contextBuilder } from "../../dist/di.js" | ||
import { ContextBuilder } from "../../dist/di.js" | ||
describe(`contextBuilder`, () => { | ||
describe(`ContextBuilder`, () => { | ||
it(`allows to register and invoke creators`, () => { | ||
const { foo } = contextBuilder() | ||
const { foo } = new ContextBuilder() | ||
.register(`foo`, () => 1) | ||
@@ -13,4 +13,4 @@ .build() | ||
it(`does not call a creator until requested`, () => { | ||
const { foo } = contextBuilder() | ||
it(`by default, does not call a creator until requested`, () => { | ||
const { foo } = new ContextBuilder() | ||
.register(`error`, () => { | ||
@@ -25,4 +25,14 @@ throw Error() | ||
it(`call every creator during building the context eagerly`, () => { | ||
expect(() => | ||
new ContextBuilder() | ||
.register(`error`, () => { | ||
throw Error("creator error") | ||
}) | ||
.build({ eagerly: true }), | ||
).toThrow(/creator error/) | ||
}) | ||
it(`allow registring creators in any order`, () => { | ||
const { foo } = contextBuilder() | ||
const { foo } = new ContextBuilder() | ||
.register(`foo`, ({ bar }) => bar + 1) | ||
@@ -36,3 +46,3 @@ .register(`bar`, () => 1) | ||
it(`fails when creator is not a function`, () => { | ||
expect(() => contextBuilder().register(`foo`, 1)).toThrow( | ||
expect(() => new ContextBuilder().register(`foo`, 1)).toThrow( | ||
/The creator of "foo" is not a function/, | ||
@@ -44,3 +54,3 @@ ) | ||
expect(() => | ||
contextBuilder() | ||
new ContextBuilder() | ||
.register(`foo`, () => 1) | ||
@@ -53,3 +63,3 @@ .register(`foo`, () => 1), | ||
let counter = 0 | ||
const context = contextBuilder() | ||
const context = new ContextBuilder() | ||
.register(`foo`, () => { | ||
@@ -56,0 +66,0 @@ counter += 1 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
465928
41
12156