From b3b0df9e1a3fb47c6baad96807692713848bb48e Mon Sep 17 00:00:00 2001 From: thebestnom Date: Wed, 6 Oct 2021 00:28:46 +0300 Subject: [PATCH 1/2] Add type assertion to is functions --- core/src/graph-types.ts | 10 ++--- core/src/integer.ts | 86 +++++++++++++++++++------------------- core/src/internal/util.ts | 2 +- core/src/spatial-types.ts | 2 +- core/src/temporal-types.ts | 6 +-- 5 files changed, 53 insertions(+), 53 deletions(-) diff --git a/core/src/graph-types.ts b/core/src/graph-types.ts index 1526caac1..ea6e5c83e 100644 --- a/core/src/graph-types.ts +++ b/core/src/graph-types.ts @@ -105,7 +105,7 @@ Object.defineProperty( * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link Node}, `false` otherwise. */ -function isNode(obj: object): boolean { +function isNode(obj: object): obj is Node { return hasIdentifierProperty(obj, NODE_IDENTIFIER_PROPERTY) } @@ -186,7 +186,7 @@ Object.defineProperty( * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link Relationship}, `false` otherwise. */ -function isRelationship(obj: object): boolean { +function isRelationship(obj: object): obj is Relationship { return hasIdentifierProperty(obj, RELATIONSHIP_IDENTIFIER_PROPERTY) } @@ -272,7 +272,7 @@ Object.defineProperty( * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link UnboundRelationship}, `false` otherwise. */ -function isUnboundRelationship(obj: object): boolean { +function isUnboundRelationship(obj: object): obj is UnboundRelationship { return hasIdentifierProperty(obj, UNBOUND_RELATIONSHIP_IDENTIFIER_PROPERTY) } @@ -320,7 +320,7 @@ Object.defineProperty( * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link PathSegment}, `false` otherwise. */ -function isPathSegment(obj: object): boolean { +function isPathSegment(obj: object): obj is PathSegment { return hasIdentifierProperty(obj, PATH_SEGMENT_IDENTIFIER_PROPERTY) } @@ -374,7 +374,7 @@ Object.defineProperty( * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link Path}, `false` otherwise. */ -function isPath(obj: object): boolean { +function isPath(obj: object): obj is Path { return hasIdentifierProperty(obj, PATH_IDENTIFIER_PROPERTY) } diff --git a/core/src/integer.ts b/core/src/integer.ts index 03ff8b6ba..98fd430f4 100644 --- a/core/src/integer.ts +++ b/core/src/integer.ts @@ -49,7 +49,7 @@ class Integer { low: number high: number - constructor (low?: number, high?: number) { + constructor(low?: number, high?: number) { /** * The low 32 bits as a signed value. * @type {number} @@ -82,7 +82,7 @@ class Integer { // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from* // methods on which they depend. - inSafeRange (): boolean { + inSafeRange(): boolean { return ( this.greaterThanOrEqual(Integer.MIN_SAFE_VALUE) && this.lessThanOrEqual(Integer.MAX_SAFE_VALUE) @@ -94,7 +94,7 @@ class Integer { * @returns {number} * @expose */ - toInt (): number { + toInt(): number { return this.low } @@ -103,7 +103,7 @@ class Integer { * @returns {number} * @expose */ - toNumber (): number { + toNumber(): number { return this.high * TWO_PWR_32_DBL + (this.low >>> 0) } @@ -112,7 +112,7 @@ class Integer { * @returns {bigint} * @expose */ - toBigInt (): bigint { + toBigInt(): bigint { if (this.isZero()) { return BigInt(0) } else if (this.isPositive()) { @@ -135,7 +135,7 @@ class Integer { * @return {number} * @package */ - toNumberOrInfinity (): number { + toNumberOrInfinity(): number { if (this.lessThan(Integer.MIN_SAFE_VALUE)) { return Number.NEGATIVE_INFINITY } else if (this.greaterThan(Integer.MAX_SAFE_VALUE)) { @@ -153,7 +153,7 @@ class Integer { * @throws {RangeError} If `radix` is out of range * @expose */ - toString (radix?: number): string { + toString(radix?: number): string { radix = radix || 10 if (radix < 2 || radix > 36) { throw RangeError('radix out of range: ' + radix) @@ -201,7 +201,7 @@ class Integer { * @returns {number} Signed high bits * @expose */ - getHighBits (): number { + getHighBits(): number { return this.high } @@ -210,7 +210,7 @@ class Integer { * @returns {number} Signed low bits * @expose */ - getLowBits (): number { + getLowBits(): number { return this.low } @@ -219,7 +219,7 @@ class Integer { * @returns {number} * @expose */ - getNumBitsAbs (): number { + getNumBitsAbs(): number { if (this.isNegative()) { return this.equals(Integer.MIN_VALUE) ? 64 : this.negate().getNumBitsAbs() } @@ -237,7 +237,7 @@ class Integer { * @returns {boolean} * @expose */ - isZero (): boolean { + isZero(): boolean { return this.high === 0 && this.low === 0 } @@ -246,7 +246,7 @@ class Integer { * @returns {boolean} * @expose */ - isNegative (): boolean { + isNegative(): boolean { return this.high < 0 } @@ -255,7 +255,7 @@ class Integer { * @returns {boolean} * @expose */ - isPositive (): boolean { + isPositive(): boolean { return this.high >= 0 } @@ -264,7 +264,7 @@ class Integer { * @returns {boolean} * @expose */ - isOdd (): boolean { + isOdd(): boolean { return (this.low & 1) === 1 } @@ -273,7 +273,7 @@ class Integer { * @returns {boolean} * @expose */ - isEven (): boolean { + isEven(): boolean { return (this.low & 1) === 0 } @@ -283,7 +283,7 @@ class Integer { * @returns {boolean} * @expose */ - equals (other: Integerable): boolean { + equals(other: Integerable): boolean { const theOther = Integer.fromValue(other) return this.high === theOther.high && this.low === theOther.low } @@ -294,7 +294,7 @@ class Integer { * @returns {boolean} * @expose */ - notEquals (other: Integerable): boolean { + notEquals(other: Integerable): boolean { return !this.equals(/* validates */ other) } @@ -304,7 +304,7 @@ class Integer { * @returns {boolean} * @expose */ - lessThan (other: Integerable): boolean { + lessThan(other: Integerable): boolean { return this.compare(/* validates */ other) < 0 } @@ -314,7 +314,7 @@ class Integer { * @returns {boolean} * @expose */ - lessThanOrEqual (other: Integerable): boolean { + lessThanOrEqual(other: Integerable): boolean { return this.compare(/* validates */ other) <= 0 } @@ -324,7 +324,7 @@ class Integer { * @returns {boolean} * @expose */ - greaterThan (other: Integerable): boolean { + greaterThan(other: Integerable): boolean { return this.compare(/* validates */ other) > 0 } @@ -334,7 +334,7 @@ class Integer { * @returns {boolean} * @expose */ - greaterThanOrEqual (other: Integerable): boolean { + greaterThanOrEqual(other: Integerable): boolean { return this.compare(/* validates */ other) >= 0 } @@ -345,7 +345,7 @@ class Integer { * if the given one is greater * @expose */ - compare (other: Integerable): number { + compare(other: Integerable): number { const theOther = Integer.fromValue(other) if (this.equals(theOther)) { @@ -368,7 +368,7 @@ class Integer { * @returns {!Integer} Negated Integer * @expose */ - negate (): Integer { + negate(): Integer { if (this.equals(Integer.MIN_VALUE)) { return Integer.MIN_VALUE } @@ -381,7 +381,7 @@ class Integer { * @returns {!Integer} Sum * @expose */ - add (addend: Integerable): Integer { + add(addend: Integerable): Integer { const theAddend = Integer.fromValue(addend) // Divide each number into 4 chunks of 16 bits, and then sum the chunks. @@ -420,7 +420,7 @@ class Integer { * @returns {!Integer} Difference * @expose */ - subtract (subtrahend: Integerable): Integer { + subtract(subtrahend: Integerable): Integer { const theSubtrahend = Integer.fromValue(subtrahend) return this.add(theSubtrahend.negate()) } @@ -431,7 +431,7 @@ class Integer { * @returns {!Integer} Product * @expose */ - multiply (multiplier: Integerable): Integer { + multiply(multiplier: Integerable): Integer { if (this.isZero()) { return Integer.ZERO } @@ -511,7 +511,7 @@ class Integer { * @returns {!Integer} Quotient * @expose */ - div (divisor: Integerable): Integer { + div(divisor: Integerable): Integer { const theDivisor = Integer.fromValue(divisor) if (theDivisor.isZero()) { @@ -601,7 +601,7 @@ class Integer { * @returns {!Integer} Remainder * @expose */ - modulo (divisor: Integerable): Integer { + modulo(divisor: Integerable): Integer { const theDivisor = Integer.fromValue(divisor) return this.subtract(this.div(theDivisor).multiply(theDivisor)) } @@ -611,7 +611,7 @@ class Integer { * @returns {!Integer} * @expose */ - not (): Integer { + not(): Integer { return Integer.fromBits(~this.low, ~this.high) } @@ -621,7 +621,7 @@ class Integer { * @returns {!Integer} * @expose */ - and (other: Integerable): Integer { + and(other: Integerable): Integer { const theOther = Integer.fromValue(other) return Integer.fromBits(this.low & theOther.low, this.high & theOther.high) } @@ -632,7 +632,7 @@ class Integer { * @returns {!Integer} * @expose */ - or (other: Integerable): Integer { + or(other: Integerable): Integer { const theOther = Integer.fromValue(other) return Integer.fromBits(this.low | theOther.low, this.high | theOther.high) } @@ -643,7 +643,7 @@ class Integer { * @returns {!Integer} * @expose */ - xor (other: Integerable): Integer { + xor(other: Integerable): Integer { const theOther = Integer.fromValue(other) return Integer.fromBits(this.low ^ theOther.low, this.high ^ theOther.high) } @@ -654,7 +654,7 @@ class Integer { * @returns {!Integer} Shifted Integer * @expose */ - shiftLeft (numBits: number | Integer): Integer { + shiftLeft(numBits: number | Integer): Integer { let bitsCount = Integer.toNumber(numBits) if ((bitsCount &= 63) === 0) { return Integer.ZERO @@ -674,7 +674,7 @@ class Integer { * @returns {!Integer} Shifted Integer * @expose */ - shiftRight (numBits: number | Integer): Integer { + shiftRight(numBits: number | Integer): Integer { let bitsCount: number = Integer.toNumber(numBits) if ((bitsCount &= 63) === 0) { @@ -763,7 +763,7 @@ class Integer { * @returns {boolean} * @expose */ - static isInteger (obj: any): boolean { + static isInteger(obj: any): obj is Integer { return (obj && obj.__isInteger__) === true } @@ -774,7 +774,7 @@ class Integer { * @returns {!Integer} The corresponding Integer value * @expose */ - static fromInt (value: number): Integer { + static fromInt(value: number): Integer { var obj, cachedObj value = value | 0 if (value >= -128 && value < 128) { @@ -799,7 +799,7 @@ class Integer { * @returns {!Integer} The corresponding Integer value * @expose */ - static fromBits (lowBits: number, highBits: number): Integer { + static fromBits(lowBits: number, highBits: number): Integer { return new Integer(lowBits, highBits) } @@ -810,7 +810,7 @@ class Integer { * @returns {!Integer} The corresponding Integer value * @expose */ - static fromNumber (value: number): Integer { + static fromNumber(value: number): Integer { if (isNaN(value) || !isFinite(value)) { return Integer.ZERO } @@ -834,7 +834,7 @@ class Integer { * @returns {!Integer} The corresponding Integer value * @expose */ - static fromString (str: string, radix?: number): Integer { + static fromString(str: string, radix?: number): Integer { if (str.length === 0) { throw newError('number format error: empty string') } @@ -884,7 +884,7 @@ class Integer { * @returns {!Integer} * @expose */ - static fromValue (val: Integerable): Integer { + static fromValue(val: Integerable): Integer { if (val /* is compatible */ instanceof Integer) { return val } @@ -908,7 +908,7 @@ class Integer { * @returns {number} * @expose */ - static toNumber (val: Integerable): number { + static toNumber(val: Integerable): number { switch (typeof val) { case 'number': return val @@ -927,7 +927,7 @@ class Integer { * @returns {string} * @expose */ - static toString (val: Integerable, radix?: number): string { + static toString(val: Integerable, radix?: number): string { return Integer.fromValue(val).toString(radix) } @@ -939,7 +939,7 @@ class Integer { * @returns {boolean} * @expose */ - static inSafeRange (val: Integerable): boolean { + static inSafeRange(val: Integerable): boolean { return Integer.fromValue(val).inSafeRange() } } diff --git a/core/src/internal/util.ts b/core/src/internal/util.ts index b87e3c1dd..509b2027c 100644 --- a/core/src/internal/util.ts +++ b/core/src/internal/util.ts @@ -219,7 +219,7 @@ function assertQueryParameters(obj: any): void { * @param str The string * @returns {boolean} True if the supplied object is an string */ -function isString(str: any): boolean { +function isString(str: any): str is string { return Object.prototype.toString.call(str) === '[object String]' } diff --git a/core/src/spatial-types.ts b/core/src/spatial-types.ts index 3de918736..b8e67750b 100644 --- a/core/src/spatial-types.ts +++ b/core/src/spatial-types.ts @@ -93,6 +93,6 @@ Object.defineProperty(Point.prototype, POINT_IDENTIFIER_PROPERTY, { * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link Point}, `false` otherwise. */ -export function isPoint(obj?: any): boolean { +export function isPoint(obj?: any): obj is Point { return (obj && obj[POINT_IDENTIFIER_PROPERTY]) === true } diff --git a/core/src/temporal-types.ts b/core/src/temporal-types.ts index 792bb6aef..a120e464f 100644 --- a/core/src/temporal-types.ts +++ b/core/src/temporal-types.ts @@ -108,7 +108,7 @@ Object.defineProperty( * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link Duration}, `false` otherwise. */ -export function isDuration(obj: object): boolean { +export function isDuration(obj: object): obj is Duration { return hasIdentifierProperty(obj, DURATION_IDENTIFIER_PROPERTY) } @@ -315,7 +315,7 @@ Object.defineProperty( * @param {Object} obj the object to test. * @return {boolean} `true` if given object is a {@link Time}, `false` otherwise. */ -export function isTime(obj: object): boolean { +export function isTime(obj: object): obj is Time { return hasIdentifierProperty(obj, TIME_IDENTIFIER_PROPERTY) } @@ -511,7 +511,7 @@ Object.defineProperty( * @param {Object} obj - The object to test. * @return {boolean} `true` if given object is a {@link LocalDateTime}, `false` otherwise. */ -export function isLocalDateTime(obj: any): boolean { +export function isLocalDateTime(obj: any): obj is LocalDateTime { return hasIdentifierProperty(obj, LOCAL_DATE_TIME_IDENTIFIER_PROPERTY) } From 88faa794f68a9b38086a8217592799d13e87dc9a Mon Sep 17 00:00:00 2001 From: thebestnom Date: Sun, 10 Oct 2021 22:41:04 +0300 Subject: [PATCH 2/2] add graph types test --- test/types/graph-types.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/types/graph-types.test.ts b/test/types/graph-types.test.ts index 78e64899d..e9e3de24d 100644 --- a/test/types/graph-types.test.ts +++ b/test/types/graph-types.test.ts @@ -16,6 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* eslint-disable no-unused-vars */ import { int, @@ -39,6 +40,10 @@ const node1Labels: string[] = node1.labels const node1Props: object = node1.properties const isNode1: boolean = node1 instanceof Node const isNode1B: boolean = isNode(node1) +const nodeAsObj: object = node1 +if (isNode(nodeAsObj)) { + const test = nodeAsObj.properties +} const node2: Node = new Node(2, ['Person', 'Employee'], { name: 'Alice' @@ -56,6 +61,10 @@ const rel1Type: string = rel1.type const rel1Props: object = rel1.properties const isRel1: boolean = rel1 instanceof Relationship const isRel1B: boolean = isRelationship(rel1) +const relationshipAsObj: object = rel1 +if (isRelationship(relationshipAsObj)) { + const test = relationshipAsObj.properties +} const rel2: UnboundRelationship = new UnboundRelationship(int(1), 'KNOWS', { since: 12345 @@ -67,6 +76,10 @@ const rel2Type: string = rel2.type const rel2Props: object = rel2.properties const isRel2: boolean = rel2 instanceof UnboundRelationship const isRel2B: boolean = isUnboundRelationship(rel2) +const unboundRelationshipAsObj: object = rel2 +if (isUnboundRelationship(unboundRelationshipAsObj)) { + const test = unboundRelationshipAsObj.properties +} const rel4: Relationship = new Relationship(2, 3, 4, 'KNOWS', { since: 12345 @@ -92,6 +105,10 @@ const pathSegment1Rel: Relationship = pathSegment1.relationship const pathSegment1End: Node = pathSegment1.end const isPathSegment1: boolean = pathSegment1 instanceof PathSegment const isPathSegment1B: boolean = isPathSegment(pathSegment1) +const pathSegmentAsObj: object = pathSegment1 +if (isPathSegment(pathSegmentAsObj)) { + const test = pathSegmentAsObj.start +} const pathSegment2: PathSegment = new PathSegment(node2, rel4, node2) const pathSegment2Start: Node = pathSegment2.start @@ -105,6 +122,10 @@ const path1Segments: PathSegment[] = path1.segments const path1Length: number = path1.length const isPath1: boolean = path1 instanceof Path const isPath1B: boolean = isPath(path1) +const pathAsObj: object = path1 +if (isPath(pathAsObj)) { + const test = pathAsObj.segments +} const path2: Path = new Path(node2, node2, [pathSegment2]) const path2Start: Node = path2.start