diff --git a/js/src/__tests__/index.test.ts b/js/src/__tests__/index.test.ts index 0d2645f..b2e6da5 100644 --- a/js/src/__tests__/index.test.ts +++ b/js/src/__tests__/index.test.ts @@ -159,10 +159,12 @@ describe('Basin', () => { basin.setCursor({ jsonPath: 'list[0]', p: -1 }) basin.setCursor({ jsonPath: 'list[1]', p: -1 }, '1') basin.setCursor({ jsonPath: 'list[2]', p: -1 }, '2') - expect(basin.write("there")).to.deep.equal({ list: ["Hello there", "Hi ", "Hey "] }) - expect(basin.write("guy", '1')).to.deep.equal({ list: ["Hello there", "Hi you", "Hey "] }) - expect(basin.write("you", '2')).to.deep.equal({ list: ["Hello there", "Hi you", "Hey you"] }) - expect(basin.write(".", '1')).to.deep.equal({ list: ["Hello there.", "Hi you.", "Hey you"] }) - expect(basin.write("!", '2')).to.deep.equal({ list: ["Hello there.", "Hi you.", "Hey you!"] }) + expect(basin.write("there")).to.deep.equal(["Hello there", "Hi ", "Hey "]) + expect(basin.write("guy", '1')).to.deep.equal(["Hello there", "Hi guy", "Hey "]) + expect(basin.write("you", '2')).to.deep.equal(["Hello there", "Hi guy", "Hey you"]) + expect(basin.write(".")).to.deep.equal(["Hello there.", "Hi guy", "Hey you"] ) + expect(basin.write(".", '1')).to.deep.equal(["Hello there.", "Hi guy.", "Hey you"] ) + expect(basin.write("!", '2')).to.deep.equal(["Hello there.", "Hi guy.", "Hey you!"]) + expect(basin.items).to.deep.equal({ list: ["Hello there.", "Hi guy.", "Hey you!"] }) }) }) \ No newline at end of file diff --git a/js/src/index.ts b/js/src/index.ts index f044ba0..8cfeb80 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -43,7 +43,8 @@ export class BasinCursor { * @typeparam T The type of values (top level) that will be modified. */ export class Basin { - private _currentKey?: T + private readonly _cursors = new Map() + private readonly _keys = new Map() /** * @param items The items to contain. If not provided, an empty object will be created. @@ -51,9 +52,9 @@ export class Basin { */ public constructor( public items: any = {}, - private _cursor?: BasinCursor) { - if (_cursor !== undefined) { - this.setCursor(_cursor) + cursor?: BasinCursor) { + if (cursor !== undefined) { + this.setCursor(cursor) } } @@ -86,7 +87,7 @@ export class Basin { * @param cursor The cursor to use. */ public setCursor(cursor: BasinCursor, label?: string): void { - this._cursor = cursor + this._cursors.set(label, cursor) if (cursor.j !== undefined) { cursor.jsonPath = cursor.j delete cursor.j @@ -103,7 +104,7 @@ export class Basin { const expressions = jp.parse(cursor.jsonPath!) for (const expression of expressions) { if (expression.expression.type !== 'root') { - this._currentKey = expression.expression.value + this._keys.set(label, expression.expression.value) break } } @@ -117,7 +118,7 @@ export class Basin { */ public write(value?: any, cursorLabel?: string): T { // For efficiency, assume the cursor is set. - const cursor = this._cursor! + const cursor = this._cursors.get(cursorLabel)! const position = cursor.position const jsonPath = cursor.jsonPath! if (typeof position !== 'number') { @@ -152,6 +153,7 @@ export class Basin { }) } - return this.items[this._currentKey] + const key = this._keys.get(cursorLabel)! + return this.items[key] } } \ No newline at end of file