-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cactus-common): add Objects utility class to get owned and inher…
…ited methods of class instance Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Utility class responsible for common and tedious tasks involving Javascript objects (instances of classes). | ||
*/ | ||
export class Objects { | ||
/** | ||
* Returns a list of methods for an instance, including the inherited ones. | ||
* Example: | ||
* | ||
* ```javascript | ||
* class Base { | ||
* constructor() { | ||
* } | ||
* getX() { | ||
* return 'x'; | ||
* } | ||
* } | ||
* | ||
* class A extends Base { | ||
* getA() { | ||
* return 'a'; | ||
* } | ||
* } | ||
* | ||
* const a = new A(); | ||
* const methodNames = Objects.getAllMethodNames(a); | ||
* console.log(methodNames); | ||
* // [ 'getA', 'getX' ] | ||
* ``` | ||
* | ||
* @param anObject | ||
*/ | ||
public static getAllMethodNames(anObject: any): string[] { | ||
let properties: string[] = []; | ||
do { | ||
const symbols = Object.getOwnPropertySymbols(anObject); | ||
const symbolPropertyNames = symbols.map((aSymbol) => aSymbol.toString()); | ||
|
||
const propertyNamesCurrent = Object.getOwnPropertyNames(anObject) | ||
.concat(symbolPropertyNames) | ||
.sort() | ||
.filter((propertyName: string, index: number, arr) => { | ||
return ( | ||
typeof anObject[propertyName] === "function" && | ||
propertyName !== "constructor" && | ||
(index === 0 || propertyName !== arr[index - 1]) && | ||
properties.indexOf(propertyName) === -1 | ||
); | ||
}); | ||
|
||
properties = properties.concat(propertyNamesCurrent); | ||
anObject = Object.getPrototypeOf(anObject); | ||
} while (anObject && Object.getPrototypeOf(anObject)); | ||
return properties; | ||
} | ||
|
||
public static getAllFieldNames(anObject: any): string[] { | ||
const allFieldNames = []; | ||
for (const propertyKey in anObject) { | ||
if (anObject.hasOwnProperty(propertyKey)) { | ||
allFieldNames.push(propertyKey); | ||
} | ||
} | ||
return allFieldNames; | ||
} | ||
} |
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,3 +1,4 @@ | ||
export { LoggerProvider } from "./logging/logger-provider"; | ||
export { Logger, ILoggerOptions } from "./logging/logger"; | ||
export { LogLevelDesc } from "loglevel"; | ||
export { Objects } from "./objects"; |
48 changes: 48 additions & 0 deletions
48
packages/cactus-common/src/test/typescript/unit/objects/get-all-method-names.ts
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,48 @@ | ||
// tslint:disable: max-classes-per-file | ||
// tslint:disable-next-line: no-var-requires | ||
const tap = require("tap"); | ||
import { Objects } from "../../../../main/typescript/public-api"; | ||
|
||
class Base { | ||
private readonly y: string; | ||
|
||
constructor() { | ||
this.y = "y"; | ||
} | ||
|
||
getX() { | ||
return "x"; | ||
} | ||
} | ||
|
||
class A extends Base { | ||
private readonly b: string; | ||
|
||
constructor() { | ||
super(); | ||
this.b = "b"; | ||
} | ||
|
||
getA() { | ||
return "a"; | ||
} | ||
} | ||
|
||
tap.test("handles inheritance correctly", (assert: any) => { | ||
const a = new A(); | ||
const methodNames = Objects.getAllMethodNames(a); | ||
assert.ok(Array.isArray(methodNames), "expect an arran of strings returned"); | ||
assert.ok( | ||
methodNames.length === 2, | ||
"expect two items in said array of strings" | ||
); | ||
assert.ok( | ||
methodNames.includes("getX"), | ||
'expect "getX" in said array of strings' | ||
); | ||
assert.ok( | ||
methodNames.includes("getA"), | ||
'expect "getA" in said array of strings' | ||
); | ||
assert.end(); | ||
}); |