Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7994,6 +7994,9 @@ i.e. before the `'ready'` event is emitted.
<!-- YAML
added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67716
description: Added `Stats.isStats` static method.
- version: v26.2.0
pr-url: https://github.com/nodejs/node/pull/60789
description: Added `Temporal.Instant` support.
Expand Down Expand Up @@ -8084,6 +8087,44 @@ BigIntStats {
}
```

### Static method: `Stats.isStats(value)`

<!-- YAML
added: REPLACEME
-->

* `value` {any}
* Returns: {boolean}

Returns `true` if `value` is a `fs.Stats` object, including objects returned
when `bigint` option was set to `true`. Returns `false` otherwise.

Prefer this method over `value instanceof fs.Stats`.

```mjs
import { statSync, Stats } from 'node:fs';

const stats = statSync('/etc/hosts');
const statsBigInt = statSync('/etc/hosts', { bigint: true });

Stats.isStats(stats); // true
Stats.isStats(statsBigInt); // true
stats instanceof Stats; // true
statsBigInt instanceof Stats; // false
```

```cjs
const { statSync, Stats } = require('node:fs');

const stats = statSync('/etc/hosts');
const statsBigInt = statSync('/etc/hosts', { bigint: true });

Stats.isStats(stats); // true
Stats.isStats(statsBigInt); // true
stats instanceof Stats; // true
statsBigInt instanceof Stats; // false
```

#### `stats.isBlockDevice()`

<!-- YAML
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
DatePrototypeGetTime,
ErrorCaptureStackTrace,
FunctionPrototypeCall,
FunctionPrototypeSymbolHasInstance,
MathFloor,
MathMin,
MathRound,
Expand Down Expand Up @@ -765,6 +766,14 @@ Stats.prototype._checkModeProperty = function(property) {
return (this.mode & S_IFMT) === property;
};

/**
* @param {unknown} value
* @returns {boolean}
*/
Stats.isStats = function(value) {
return FunctionPrototypeSymbolHasInstance(StatsBase, value);
};

/**
* @param {Float64Array | BigInt64Array} stats
* @param {number} offset
Expand Down
61 changes: 61 additions & 0 deletions test/parallel/test-fs-stats-isstats.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import '../common/index.mjs';
import assert from 'node:assert/strict';
import { Stats, statSync } from 'node:fs';

const testFile = new URL(import.meta.url);
const stats = statSync(testFile);
const statsBigInt = statSync(testFile, { bigint: true });

// `isStats` shouldn't depend on receiver
const { isStats } = Stats;

assert.strictEqual(Stats.isStats(stats), true);
assert.strictEqual(Stats.isStats(statsBigInt), true);
assert.strictEqual(isStats(stats), true);
assert.strictEqual(isStats(statsBigInt), true);

for (const value of [
undefined,
null,
false,
0,
0n,
'',
Symbol(),
{},
{ __proto__: null },
[],
Promise.resolve(),
class Stats {},
{
dev: stats.dev,
mode: stats.mode,
nlink: stats.nlink,
uid: stats.uid,
gid: stats.gid,
rdev: stats.rdev,
blksize: stats.blksize,
ino: stats.ino,
size: stats.size,
blocks: stats.blocks,
atimeMs: stats.atimeMs,
mtimeMs: stats.mtimeMs,
ctimeMs: stats.ctimeMs,
birthtimeMs: stats.birthtimeMs,
atime: stats.atime,
mtime: stats.mtime,
ctime: stats.ctime,
birthtime: stats.birthtime,
_checkModeProperty: stats._checkModeProperty,
isDirectory: stats.isDirectory,
isFile: stats.isFile,
isBlockDevice: stats.isBlockDevice,
isCharacterDevice: stats.isCharacterDevice,
isSymbolicLink: stats.isSymbolicLink,
isFIFO: stats.isFIFO,
isSocket: stats.isSocket,
},
]) {
assert.strictEqual(Stats.isStats(value), false);
assert.strictEqual(isStats(value), false);
}
Loading