-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from kawanet/0.2.2
0.2.2
- Loading branch information
Showing
11 changed files
with
210 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type {NSP} from "../types/index.js"; | ||
|
||
/** | ||
* The Store class was implemented using a stack {stack: P[]} at first. | ||
* Now it was changed to a linked list to allow access to the parent. | ||
*/ | ||
export class Store<P> implements NSP.StackStore<P> { | ||
item: Item<P>; | ||
|
||
constructor(value?: P) { | ||
this.item = {value} as Item<P>; | ||
} | ||
|
||
open(value?: P): void { | ||
this.item = {parent: this.item, value}; | ||
} | ||
|
||
close(): P { | ||
const item = this.item; | ||
this.item = item.parent; | ||
return item.value; | ||
} | ||
|
||
get(): P { | ||
return this.item.value; | ||
} | ||
|
||
set(value: P): void { | ||
this.item.value = value; | ||
} | ||
|
||
find(test: (data: P) => boolean): P { | ||
let item = this.item; | ||
|
||
while (item) { | ||
if (test(item.value)) { | ||
return item.value; | ||
} | ||
item = item.parent; | ||
} | ||
} | ||
} | ||
|
||
interface Item<P> { | ||
parent: Item<P>; | ||
value: P; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {strict as assert} from "assert"; | ||
import {createNSP, NSP} from "../index.js"; | ||
|
||
const TITLE = "350.tag-class.ts"; | ||
|
||
interface TagAttr { | ||
// | ||
} | ||
|
||
describe(TITLE, () => { | ||
const nsp = createNSP(); | ||
const ctx = {}; | ||
|
||
class BodyTag implements NSP.TagClass { | ||
constructor(private tag: NSP.TagDef<TagAttr>, private context: any) { | ||
// | ||
} | ||
|
||
async render() { | ||
const body = await this.tag.body(this.context); | ||
return (body == null) ? "null" : (body === "") ? "empty" : ("string" === typeof body) ? body : typeof body; | ||
} | ||
} | ||
|
||
nsp.addTagLib({ns: "test", tag: {body: BodyTag}}); | ||
|
||
it("empty tag", async () => { | ||
assert.equal(await nsp.parse(`[<test:body/>]`).toFn()(ctx), "[null]"); | ||
}); | ||
|
||
it("empty body", async () => { | ||
assert.equal(await nsp.parse(`[<test:body></test:body>]`).toFn()(ctx), "[empty]"); | ||
}); | ||
|
||
it("string body", async () => { | ||
assert.equal(await nsp.parse(`[<test:body>FOO</test:body>]`).toFn()(ctx), "[FOO]"); | ||
}); | ||
|
||
it("nested", async () => { | ||
assert.equal(await nsp.parse(`[<test:body><test:body/></test:body>]`).toFn()(ctx), "[null]"); | ||
assert.equal(await nsp.parse(`[<test:body><test:body></test:body></test:body>]`).toFn()(ctx), "[empty]"); | ||
assert.equal(await nsp.parse(`[<test:body><test:body>FOO</test:body></test:body>]`).toFn()(ctx), "[FOO]"); | ||
}); | ||
|
||
it("unregistered tag", async () => { | ||
assert.equal(await nsp.parse(`<test:na/>`).toFn()(ctx), `<test:na/>`); | ||
assert.equal(await nsp.parse(`<test:na></test:na>`).toFn()(ctx), `<test:na></test:na>`); | ||
assert.equal(await nsp.parse(`<test:na>FOO</test:na>`).toFn()(ctx), `<test:na>FOO</test:na>`); | ||
assert.equal(await nsp.parse(`<test:na><test:na/></test:na>`).toFn()(ctx), `<test:na><test:na/></test:na>`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {strict as assert} from "assert"; | ||
import {createNSP, NSP} from "../index.js"; | ||
|
||
const TITLE = "360.function-this.ts"; | ||
|
||
describe(TITLE, () => { | ||
const nsp = createNSP(); | ||
const ctx = {}; | ||
|
||
nsp.addTagLib({ | ||
ns: "test", | ||
fn: {thisIsApp, thisIsNull} | ||
}); | ||
|
||
function thisIsApp(this: NSP.App) { | ||
return this instanceof nsp.constructor; | ||
} | ||
|
||
function thisIsNull(this: NSP.App) { | ||
return this == null; | ||
} | ||
|
||
it("test:thisIsApp()", async () => { | ||
assert.equal(nsp.parse('[${test:thisIsApp()}]').toFn()(ctx), "[true]"); | ||
}); | ||
|
||
it("test:thisIsNull()", async () => { | ||
assert.equal(nsp.parse('[${test:thisIsNull()}]').toFn()(ctx), "[false]"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters