Skip to content

Commit

Permalink
feat: add primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
florianstancioiu committed Mar 14, 2024
1 parent a962af0 commit 89de004
Showing 1 changed file with 66 additions and 28 deletions.
94 changes: 66 additions & 28 deletions src/cosmoz-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Tree {
this.searchProperty = options.searchProperty || 'name';
}

static _sortPathNodes(a, b) {
static _sortPathNodes(a, b): number {
const undefCounter = (item) => item === undefined,
defCounter = (item) => item,
aUndefCount = a.filter(undefCounter).length,
Expand All @@ -53,12 +53,15 @@ export class Tree {
if (aUndefCount < bUndefCount) {
return -1;
}

if (aUndefCount > bUndefCount || aDefCount < bDefCount) {
return 1;
}

if (aDefCount > bDefCount) {
return -1;
}

return 0;
}

Expand All @@ -70,10 +73,10 @@ export class Tree {
* @param {Array} nodes [this._roots] (The objects the search should be based on.)
*/
getNodeByProperty(
propertyValue,
propertyName = this.searchProperty,
nodes = this._roots,
) {
propertyValue?: string,
propertyName: string = this.searchProperty,
nodes: object = this._roots,
): object | undefined {
if (propertyValue === undefined) {
return;
}
Expand All @@ -89,12 +92,18 @@ export class Tree {
* @param {Boolean} exact [true] (If the search should be executed exact or flaw. true wouldn't match "Pet")
* @param {String} propertyName [this.searchProperty] (The name of the property the match should be based on. e.g. "name")
*/
searchNodes(propertyValue, nodes, exact, propertyName = this.searchProperty) {
searchNodes(
propertyValue: string,
nodes,
exact: boolean | undefined,
propertyName: string = this.searchProperty,
): object[] {
const options = {
propertyName,
exact: exact !== undefined ? exact : true,
firstHitOnly: false,
};

return this._searchNodes(propertyValue, options, nodes);
}

Expand All @@ -105,12 +114,17 @@ export class Tree {
* @param {String} propertyName [this.searchProperty] (The name of the property the match should be based on. e.g. "name")
* @param {Object} nodes [this._treeData] (The nodes the search should be based on.)
*/
findNode(propertyValue, propertyName = this.searchProperty, nodes) {
findNode(
propertyValue: string,
propertyName: string = this.searchProperty,
nodes,
) {
const options = {
propertyName,
exact: true,
firstHitOnly: true,
};

return this._searchNodes(propertyValue, options, nodes).shift();
}

Expand All @@ -124,7 +138,15 @@ export class Tree {
* @param {Boolean} options.firstHitOnly [false] (If the search should only return the first found node.)
* @param {Object} nodes [this._roots] (The nodes the search should be based on.)
*/
_searchNodes(propertyValue, options, nodes = this._roots) {
_searchNodes(
propertyValue: string,
options: {
propertyName: string;
exact: boolean;
firstHitOnly: boolean;
},
nodes = this._roots,
) {
const results = [];

for (const node of nodes) {
Expand All @@ -146,9 +168,9 @@ export class Tree {
* @param {String} pathLocatorSeparator [this.pathLocatorSeparator] (The string which separates the path. e.g ".")
*/
getNodeByPathLocator(
pathLocator,
pathLocator?: string,
nodeObj = this._treeData,
pathLocatorSeparator = this.pathLocatorSeparator,
pathLocatorSeparator: string = this.pathLocatorSeparator,
) {
if (!pathLocator) {
return this._roots;
Expand All @@ -174,9 +196,9 @@ export class Tree {
* @param {String} pathLocatorSeparator [this.pathLocatorSeparator] (The string which separates the path.)
*/
getPathNodes(
pathLocator,
pathLocator?: string,
nodeObj = this._treeData,
pathLocatorSeparator = this.pathLocatorSeparator,
pathLocatorSeparator: string = this.pathLocatorSeparator,
) {
if (!pathLocator) {
return nodeObj;
Expand All @@ -195,9 +217,9 @@ export class Tree {
}

_getPathNodes(
pathLocator,
pathLocator: string,
nodeObj = this._treeData,
pathLocatorSeparator = this.pathLocatorSeparator,
pathLocatorSeparator: string = this.pathLocatorSeparator,
) {
const path = pathLocator.split(pathLocatorSeparator),
nodes = this._pathToNodes(path, nodeObj, pathLocatorSeparator);
Expand Down Expand Up @@ -236,10 +258,10 @@ export class Tree {
* @param {String} pathLocatorSeparator [this.pathLocatorSeparator] (The string which separates the path segments of pathLocator.)
*/
getPathString(
pathLocator,
pathProperty = this.searchProperty,
pathStringSeparator = this.pathStringSeparator,
pathLocatorSeparator = this.pathLocatorSeparator,
pathLocator: string,
pathProperty: string = this.searchProperty,
pathStringSeparator: string = this.pathStringSeparator,
pathLocatorSeparator: string = this.pathLocatorSeparator,
) {
const pathNodes = this.getPathNodes(
pathLocator,
Expand Down Expand Up @@ -267,11 +289,11 @@ export class Tree {
* @param {String} pathLocatorSeparator [this.pathLocatorSeparator] (The string which separates the path. e.g ".")
*/
getPathStringByProperty(
propertyValue,
propertyName = this.searchProperty,
pathProperty = this.searchProperty,
pathStringSeparator = this.pathStringSeparator,
pathLocatorSeparator = this.pathLocatorSeparator,
propertyValue: string,
propertyName: string = this.searchProperty,
pathProperty: string = this.searchProperty,
pathStringSeparator: string = this.pathStringSeparator,
pathLocatorSeparator: string = this.pathLocatorSeparator,
) {
if (propertyValue === undefined) {
return;
Expand Down Expand Up @@ -299,7 +321,7 @@ export class Tree {
* @param {Object} node The object to return children from
* @returns {Object|Array} The node's children
*/
getChildren(node) {
getChildren(node: object) {
if (!node || !node[this.childProperty]) {
return [];
}
Expand All @@ -311,7 +333,7 @@ export class Tree {
* @param {Object} node The object to get children from
* @returns {Boolean} True if node has children
*/
hasChildren(node) {
hasChildren(node: object): boolean {
if (!node) {
return false;
}
Expand All @@ -332,7 +354,7 @@ export class Tree {
* @param {String} propertyName The name of property
* @returns {*} The value of the property
*/
getProperty(node, propertyName) {
getProperty(node: object, propertyName: string) {
if (!node || !propertyName) {
return;
}
Expand All @@ -348,7 +370,14 @@ export class Tree {
* @param {String} options.propertyName (The name of the property the match should be based on. e.g. "name")
* @param {Boolean} options.exact [false] (If the search should be executed exact or fuzzy. true wouldn't match "Pet")
*/
nodeConformsSearch(node, propertyValue, options) {
nodeConformsSearch(
node: object,
propertyValue: string,
options?: {
propertyName: string;
exact: boolean;
},
): boolean | undefined {
const property = options ? node[options.propertyName] : undefined;

if (!property) {
Expand All @@ -360,6 +389,7 @@ export class Tree {
if (options.exact) {
return property === propertyValue;
}

return property.toLowerCase().indexOf(propertyValue.toLowerCase()) > -1;
}

Expand All @@ -373,7 +403,15 @@ export class Tree {
* @param {Boolean} options.exact [false] (If false, the propertyValue is matched fuzzy)
* @param {Array} results (The array search results get added to.) Default: []
*/
search(node, propertyValue, options, results = []) {
search(
node,
propertyValue: string,
options: {
propertyName: string;
exact: string;
},
results = [],
) {
const nodeConforms = this.nodeConformsSearch(node, propertyValue, options),
children = this.getChildren(node);

Expand Down

0 comments on commit 89de004

Please sign in to comment.