-
Notifications
You must be signed in to change notification settings - Fork 27
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 #54 from Kiruse/main
WebSocketClient: fix unexpected eof + improve subscribe & query
- Loading branch information
Showing
3 changed files
with
121 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const escape = (str: string) => str.replace(/\\/g, '\\\\').replace(/'/g, "\\'"); | ||
|
||
export class TendermintQuery { | ||
private _query: string[] = []; | ||
|
||
getValue(value: number | string | Date) { | ||
if (typeof value === 'number') { | ||
return value; | ||
} else if (typeof value === 'string') { | ||
return `'${escape(value)}'`; | ||
} else { | ||
return value.toISOString(); | ||
} | ||
} | ||
|
||
exact(field: string, value: number | string | Date) { | ||
this._query.push(`${field}=${this.getValue(value)}`); | ||
return this; | ||
} | ||
|
||
compare(field: string, op: `${'<' | '>'}${'' | '='}`, value: number | Date) { | ||
this._query.push(`${field}${op}${this.getValue(value)}`); | ||
return this; | ||
} | ||
|
||
exists(field: string) { | ||
this._query.push(`${field} EXISTS`); | ||
return this; | ||
} | ||
|
||
contains(field: string, value: string) { | ||
this._query.push(`${field} CONTAINS '${escape(value)}'`); | ||
return this; | ||
} | ||
|
||
clone() { | ||
const q = new TendermintQuery(); | ||
q._query = this._query.slice(); | ||
return q; | ||
} | ||
|
||
toString() { | ||
return this._query.join(' AND '); | ||
} | ||
|
||
static AND(lhs: TendermintQuery, rhs: TendermintQuery) { | ||
const q = new TendermintQuery(); | ||
q._query.push(`(${lhs}) AND (${rhs})`); | ||
return q; | ||
} | ||
|
||
static OR(lhs: TendermintQuery, rhs: TendermintQuery) { | ||
const q = new TendermintQuery(); | ||
q._query.push(`(${lhs}) OR (${rhs})`); | ||
return q; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './hash'; | ||
export * from './contract'; | ||
export * from './TendermintQuery'; |