Skip to content

Commit

Permalink
Merge pull request #202 from dagrejs/hasOwn
Browse files Browse the repository at this point in the history
Using Object.hasOwn instead of hasOwnProperty
  • Loading branch information
rustedgrail authored Aug 15, 2024
2 parents 700bc46 + 0a8d032 commit 6f76f78
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/alg/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function components(g) {
var cmpt;

function dfs(v) {
if (visited.hasOwnProperty(v)) return;
if (Object.hasOwn(visited, v)) return;
visited[v] = true;
cmpt.push(v);
g.successors(v).forEach(dfs);
Expand Down
4 changes: 2 additions & 2 deletions lib/alg/dfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function postOrderDfs(v, navigation, visited, acc) {
if (curr[1]) {
acc.push(curr[0]);
} else {
if (!visited.hasOwnProperty(curr[0])) {
if (!Object.hasOwn(visited, curr[0])) {
visited[curr[0]] = true;
stack.push([curr[0], true]);
forEachRight(navigation(curr[0]), w => stack.push([w, false]));
Expand All @@ -49,7 +49,7 @@ function preOrderDfs(v, navigation, visited, acc) {
var stack = [v];
while (stack.length > 0) {
var curr = stack.pop();
if (!visited.hasOwnProperty(curr)) {
if (!Object.hasOwn(visited, curr)) {
visited[curr] = true;
acc.push(curr);
forEachRight(navigation(curr), w => stack.push(w));
Expand Down
2 changes: 1 addition & 1 deletion lib/alg/prim.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function prim(g, weightFunc) {
var init = false;
while (pq.size() > 0) {
v = pq.removeMin();
if (parents.hasOwnProperty(v)) {
if (Object.hasOwn(parents, v)) {
result.setEdge(v, parents[v]);
} else if (init) {
throw new Error("Input graph is not connected: " + g);
Expand Down
4 changes: 2 additions & 2 deletions lib/alg/tarjan.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function tarjan(g) {
stack.push(v);

g.successors(v).forEach(function(w) {
if (!visited.hasOwnProperty(w)) {
if (!Object.hasOwn(visited, w)) {
dfs(w);
entry.lowlink = Math.min(entry.lowlink, visited[w].lowlink);
} else if (visited[w].onStack) {
Expand All @@ -36,7 +36,7 @@ function tarjan(g) {
}

g.nodes().forEach(function(v) {
if (!visited.hasOwnProperty(v)) {
if (!Object.hasOwn(visited, v)) {
dfs(v);
}
});
Expand Down
4 changes: 2 additions & 2 deletions lib/alg/topsort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ function topsort(g) {
var results = [];

function visit(node) {
if (stack.hasOwnProperty(node)) {
if (Object.hasOwn(stack, node)) {
throw new CycleException();
}

if (!visited.hasOwnProperty(node)) {
if (!Object.hasOwn(visited, node)) {
stack[node] = true;
visited[node] = true;
g.predecessors(node).forEach(visit);
Expand Down
4 changes: 2 additions & 2 deletions lib/data/priority-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PriorityQueue {
* Returns `true` if **key** is in the queue and `false` if not.
*/
has(key) {
return this._keyIndices.hasOwnProperty(key);
return Object.hasOwn(this._keyIndices, key);
}

/**
Expand Down Expand Up @@ -65,7 +65,7 @@ class PriorityQueue {
add(key, priority) {
var keyIndices = this._keyIndices;
key = String(key);
if (!keyIndices.hasOwnProperty(key)) {
if (!Object.hasOwn(keyIndices, key)) {
var arr = this._arr;
var index = arr.length;
keyIndices[key] = index;
Expand Down
16 changes: 8 additions & 8 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Graph {

constructor(opts) {
if (opts) {
this._isDirected = opts.hasOwnProperty("directed") ? opts.directed : true;
this._isMultigraph = opts.hasOwnProperty("multigraph") ? opts.multigraph : false;
this._isCompound = opts.hasOwnProperty("compound") ? opts.compound : false;
this._isDirected = Object.hasOwn(opts, "directed") ? opts.directed : true;
this._isMultigraph = Object.hasOwn(opts, "multigraph") ? opts.multigraph : false;
this._isCompound = Object.hasOwn(opts, "compound") ? opts.compound : false;
}

if (this._isCompound) {
Expand Down Expand Up @@ -192,7 +192,7 @@ class Graph {
* Complexity: O(1).
*/
setNode(v, value) {
if (this._nodes.hasOwnProperty(v)) {
if (Object.hasOwn(this._nodes, v)) {
if (arguments.length > 1) {
this._nodes[v] = value;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ class Graph {
* Detects whether graph has a node with specified name or not.
*/
hasNode(v) {
return this._nodes.hasOwnProperty(v);
return Object.hasOwn(this._nodes, v);
}

/**
Expand All @@ -236,7 +236,7 @@ class Graph {
*/
removeNode(v) {
var self = this;
if (this._nodes.hasOwnProperty(v)) {
if (Object.hasOwn(this._nodes, v)) {
var removeEdge = e => self.removeEdge(self._edgeObjs[e]);
delete this._nodes[v];
if (this._isCompound) {
Expand Down Expand Up @@ -514,7 +514,7 @@ class Graph {
}

var e = edgeArgsToId(this._isDirected, v, w, name);
if (this._edgeLabels.hasOwnProperty(e)) {
if (Object.hasOwn(this._edgeLabels, e)) {
if (valueSpecified) {
this._edgeLabels[e] = value;
}
Expand Down Expand Up @@ -579,7 +579,7 @@ class Graph {
var e = (arguments.length === 1
? edgeObjToId(this._isDirected, arguments[0])
: edgeArgsToId(this._isDirected, v, w, name));
return this._edgeLabels.hasOwnProperty(e);
return Object.hasOwn(this._edgeLabels, e);
}

/**
Expand Down

0 comments on commit 6f76f78

Please sign in to comment.