@lopatnov/namespace
Advanced tools
Comparing version 1.1.0 to 1.2.0
112
package.json
{ | ||
"name": "@lopatnov/namespace", | ||
"version": "1.1.0", | ||
"license": "Apache-2.0", | ||
"author": "lopatnov", | ||
"description": "Demonstrates a sample library template", | ||
"homepage": "https://lopatnov.github.io/namespace/", | ||
"keywords": [ | ||
"TypeScript", | ||
"JavaScript", | ||
"library", | ||
"namespace", | ||
"javascript-library", | ||
"namespace" | ||
], | ||
"umdName": "Namespace", | ||
"libraryFile": "namespace", | ||
"main": "dist/namespace.js", | ||
"module": "dist/namespace.es.js", | ||
"types": "dist/namespace.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/lopatnov/namespace.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/lopatnov/namespace/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup -c rollup.config.ts", | ||
"watch": "rollup -cw rollup.config.ts", | ||
"configure-npm": "node ./set-registry.js -s https://registry.npmjs.org", | ||
"configure-gpr": "node ./set-registry.js -s https://npm.pkg.github.com", | ||
"test": "jest" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
"devDependencies": { | ||
"@lopatnov/rollup-plugin-uglify": "^2.1.0", | ||
"@types/jest": "^26.0.9", | ||
"@types/typescript": "^2.0.0", | ||
"jest": "^26.2.2", | ||
"jest-config": "^26.2.2", | ||
"rollup": "^2.23.1", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-json": "^4.0.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-sourcemaps": "^0.6.2", | ||
"rollup-plugin-typescript2": "^0.27.2", | ||
"terser": "^5.0.0", | ||
"ts-jest": "^26.1.4", | ||
"typescript": "^3.9.7" | ||
} | ||
} | ||
"name": "@lopatnov/namespace", | ||
"version": "1.2.0", | ||
"license": "Apache-2.0", | ||
"author": "lopatnov", | ||
"description": "Dynamic namespace creation", | ||
"homepage": "https://lopatnov.github.io/namespace/", | ||
"keywords": [ | ||
"TypeScript", | ||
"JavaScript", | ||
"library", | ||
"namespace", | ||
"javascript-library" | ||
], | ||
"umdName": "Namespace", | ||
"libraryFile": "namespace", | ||
"main": "dist/namespace.js", | ||
"module": "dist/namespace.es.js", | ||
"types": "dist/namespace.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/lopatnov/namespace.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/lopatnov/namespace/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup -c rollup.config.ts", | ||
"watch": "rollup -cw rollup.config.ts", | ||
"configure-npm": "node ./set-registry.js -s https://registry.npmjs.org", | ||
"configure-gpr": "node ./set-registry.js -s https://npm.pkg.github.com", | ||
"test": "jest" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"@lopatnov/rollup-plugin-uglify": "^2.1.0", | ||
"@types/jest": "^26.0.23", | ||
"@types/typescript": "^2.0.0", | ||
"jest": "^27.0.3", | ||
"jest-config": "^27.0.3", | ||
"rollup": "^2.50.5", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-json": "^4.0.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-sourcemaps": "^0.6.3", | ||
"rollup-plugin-typescript2": "^0.30.0", | ||
"terser": "^5.7.0", | ||
"ts-jest": "^27.0.2", | ||
"typescript": "^4.3.2" | ||
} | ||
} |
131
README.md
@@ -1,2 +0,2 @@ | ||
# @lopatnov/namespace | ||
# @lopatnov/namespace [![Twitter](https://img.shields.io/twitter/url?url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40lopatnov%2Fnamespace)](https://twitter.com/intent/tweet?text=I%20want%20to%20share%20TypeScript%20library:&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40lopatnov%2Fnamespace) | ||
@@ -16,4 +16,4 @@ [![npm](https://img.shields.io/npm/dt/@lopatnov/namespace)](https://www.npmjs.com/package/@lopatnov/namespace) | ||
[![Patreon](https://img.shields.io/badge/Donate-Patreon-informational)](https://www.patreon.com/lopatnov) | ||
[![Twitter](https://img.shields.io/twitter/url?url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40lopatnov%2Fnamespace)](https://twitter.com/intent/tweet?text=I%20want%20to%20share%20TypeScript%20library:&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40lopatnov%2Fnamespace) | ||
[![LinkedIn](https://img.shields.io/badge/LinkedIn-lopatnov-informational?style=social&logo=appveyor)](https://www.linkedin.com/in/lopatnov/) | ||
[![sobe.ru](https://img.shields.io/static/v1?label=sobe.ru&message=%D0%9A%D0%BD%D0%B8%D0%B3%D0%B8&color=yellow&logo=data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAArlBMVEUAAAD//////////////////////////////////////////////////////////////////PP/3l7/9c//0yb/zAD/6ZP/zQf/++7/3FD/88X/0h7//v7/5oX/zATUqQDktgD/5HjQpgAFBACQcwD/zw/fsgCOcQD6yADZrQD2xAD8yQDnuADxwADcsADbrwDpugD3xQD5xwDjtQDywQD+ywD9ygDvvwD7yAD/1jRaObVGAAAAEHRSTlMAA3zg707pEJP8MMUBYN5fiwXJMQAAAAFiS0dEAf8CLd4AAAAHdElNRQflBgMAAxO4O2jCAAAAuElEQVQoz42S1w7CMAxFS8ueYZgNLZuyRynw/z9GdtxIkbgPceQT6Tq2vZwfEKx8wRPyiaViSYDABqQsAMq0OzxUqhbo9kBcavUM6A9AAtJAYDgC0ID7i+t4AghwfxanszlAGBnA/Flc0MfL1doA5s/ChoLtbg8QI392gpIBzf/AwYAWAsdTrIE05/nz5Xq7S6DKpenHM0pe+o/qg5Am74/0ybTkm+q6wG4iltV2LTko52idy+Banx9RYiS6Vrsc3AAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMS0wNi0wM1QwMDowMzoxOCswMDowMLvSSCkAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjEtMDYtMDNUMDA6MDM6MTgrMDA6MDDKj/CVAAAAAElFTkSuQmCC)](https://sobe.ru/na/tech_knigi) | ||
[![LinkedIn](https://img.shields.io/badge/LinkedIn-lopatnov-informational?style=social&logo=linkedin)](https://www.linkedin.com/in/lopatnov/) | ||
@@ -52,95 +52,86 @@ Dynamic namespace creation | ||
### new operator | ||
### namespace creation | ||
```ts | ||
var n = new Namespace("Hello.World"); | ||
console.log((n as any).Hello.World instanceof Namespace); | ||
``` | ||
const ns = new Namespace(); // <-- create namespace variable | ||
```js | ||
window.globalSpace = new Namespace( | ||
"Eeny.meeny.miny.moe[Catch][a][tiger][by][the][toe]" | ||
); | ||
console.log(Eeny.meeny.miny.moe.Catch.a.tiger.by.the.toe); | ||
ns.namespace("a.b.c.d"); // <-- create inner namespaces | ||
ns.namespace("a.b.c.e"); // <-- Function namespace(path: NamespacePath): Namespace | ||
ns.namespace("a.b.f.g"); // <-- | ||
ns.namespace("a.i.h.k"); // <-- | ||
ns.namespace("a.i.h.l"); // <-- | ||
ns.namespace("a.m.n.o"); // <-- | ||
// Access to inner namespaces | ||
const anyNS: any = ns; // <-- to avoid type casting | ||
console.log(anyNS.a.b.f.g instanceof Namespace); // true | ||
console.log((ns as any).a.b.c.d instanceof Namespace); // true | ||
console.log((ns as any).a.b.c.e instanceof Namespace); // true | ||
``` | ||
### direct call | ||
#### checking that object inside namespace exists | ||
```js | ||
var n = Namespace("A.Pacific.Ocean"); // <-- without new | ||
console.log(n.A.Pacific.Ocean instanceof Namespace); | ||
```ts | ||
// Function exists(path: NamespacePath): boolean | ||
console.log(ns.exists('a.b.c.d')); // true | ||
console.log(ns.exists('a.b.c.e')); // true | ||
console.log(ns.exists('a.b.f.g')); // true | ||
console.log(ns.exists('a.i.h.k')); // true | ||
console.log(ns.exists('a.i.h.l')); // true | ||
console.log(ns.exists('a.m.n.o')); // true | ||
``` | ||
### apply namespace object to another | ||
### create namespace through direct call | ||
```ts | ||
var x = new Namespace("Games.World"); | ||
var y = {}; | ||
var z = x.goto("Games.World"); | ||
x.applyTo(y, "Hello"); | ||
console.log(z === y.Hello.Games.World); // true | ||
var dynamicNamespace: any = Namespace; // <-- escape current TypeScript restrictions with any Type | ||
var a = dynamicNamespace('Pacific.Ocean'); // <-- call as function | ||
console.log(a.Pacific.Ocean instanceof Namespace); // true | ||
``` | ||
### create namespace through new operator | ||
```ts | ||
var space: any = {}; | ||
var n = new Namespace("cruising.airliner"); | ||
n.applyTo(space, "A"); // <-- applyTo(context: any, name: string): void | ||
console.log(space.A.cruising.airliner instanceof Namespace); | ||
var n: any = new Namespace("Hello.World"); | ||
console.log(n.Hello.World instanceof Namespace); // true | ||
``` | ||
### get inner object | ||
```ts | ||
var n: any = new Namespace("Yellow.Submarine"); | ||
var y = n.goto("Yellow.Submarine"); // <-- goto(path: NamespacePath): any | ||
console.log(y === n.Yellow.Submarine); | ||
var eeny: any = new Namespace('meeny.miny.moe[Catch][a][tiger][by][the][toe]'); | ||
console.log(eeny.meeny.miny.moe.Catch.a.tiger.by.the.toe instanceof Namespace); // true | ||
``` | ||
### make inner namespace | ||
### go to inner object | ||
```ts | ||
const glob: Namespace & any = new Namespace(); | ||
var kitchenRadar = new Namespace(); | ||
kitchenRadar.namespace('big.fruits'); | ||
kitchenRadar.namespace('big.eggs'); | ||
kitchenRadar.namespace('small.dishes'); | ||
kitchenRadar.namespace('small.cookies'); | ||
function namespace(path: string) { | ||
return function ( | ||
target: any, | ||
propertyKey?: string, | ||
descriptor?: PropertyDescriptor | ||
) { | ||
const ns = glob.namespace(path); // <-- make inner namespace | ||
const name = propertyKey || target.prototype.constructor.name; | ||
ns[name] = propertyKey ? target[propertyKey] : target; | ||
}; | ||
} | ||
var fruits = kitchenRadar.goto('big.fruits'); // <-- goto(path: NamespacePath): any | ||
var eggs = kitchenRadar.goto('big.eggs'); | ||
var dishes = kitchenRadar.goto('small.dishes'); | ||
var cookies = kitchenRadar.goto('small.cookies'); | ||
@namespace("white.animals") | ||
class Actions { | ||
@namespace("white.animals") | ||
makeAlbino(animal: string) { | ||
return `${animal} is white now`; | ||
} | ||
} | ||
console.log(glob.white.animals.makeAlbino("unicorn")); // 'unicorn is white now' | ||
console.log(new glob.white.animals.Actions().makeAlbino("rose panther")); // 'rose panther is white now' | ||
console.log(fruits === (kitchenRadar as any).big.fruits); // true | ||
console.log(eggs === (kitchenRadar as any).big.eggs); // true | ||
console.log(dishes === (kitchenRadar as any).small.dishes); // true | ||
console.log(cookies === (kitchenRadar as any).small.cookies); // true | ||
``` | ||
### check if path exists | ||
### apply namespace as a property of an object | ||
```ts | ||
const ns = new Namespace(); | ||
ns.namespace("a.b.c.d"); | ||
ns.namespace("a.b.c.e"); | ||
ns.namespace("a.b.f.g"); | ||
ns.namespace("a.i.h.k"); | ||
ns.namespace("a.i.h.l"); | ||
ns.namespace("a.m.n.o"); | ||
var persons: any = {}; | ||
var extension = new Namespace('with.small.kitty'); | ||
console.log(ns.exists('a.b.c.d')); | ||
console.log(ns.exists('a.b.c.e')); | ||
console.log(ns.exists('a.b.f.g')); | ||
console.log(ns.exists('a.i.h.k')); | ||
console.log(ns.exists('a.i.h.l')); | ||
console.log(ns.exists('a.m.n.o')); | ||
extension.applyTo(persons, 'Julia'); // Function applyTo(context: any, name: string): void | ||
extension.applyTo(persons, 'Kathy'); | ||
extension.applyTo(persons, 'Liza'); | ||
console.log(persons.Julia.with.small.kitty instanceof Namespace); // true | ||
console.log(persons.Kathy.with.small.kitty instanceof Namespace); // true | ||
console.log(persons.Liza.with.small.kitty instanceof Namespace); // true | ||
``` | ||
@@ -158,2 +149,2 @@ | ||
Copyright 2020 Oleksandr Lopatnov | ||
Copyright 2020–2021 Oleksandr Lopatnov |
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
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
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
50674
0
100
0
12
148