Skip to content

Instantly share code, notes, and snippets.

@Offirmo
Last active January 16, 2025 00:42
Show Gist options
  • Save Offirmo/a1e78aea8ae642c43d782e2d560df51f to your computer and use it in GitHub Desktop.
Save Offirmo/a1e78aea8ae642c43d782e2d560df51f to your computer and use it in GitHub Desktop.
[JS -- snippets -- node] #JavaScript #nodejs
/////////////////////////////////////////////////
// __dirname, __filename
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#pure-esm-package
// https://nodejs.org/api/globals.html
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const some_file = path.join(__dirname, './foo.png')
/////////////////////////////////////////////////
import { strict as assert } from 'node:assert';
new assert.AssertionError({
actual: 1,
expected: 2,
operator: 'strictEqual'
});
assert(value[, message])
assert.ok(value[, message])
assert.deepStrictEqual(actual, expected[, message])
assert.notDeepStrictEqual(actual, expected[, message])
assert.doesNotReject(asyncFn[, error][, message])
assert.doesNotThrow(fn[, error][, message])
assert.strictEqual(actual, expected[, message])
assert.notStrictEqual(actual, expected[, message])
assert.fail([message])
/////////////////////////////////////////////////
// Read a file
import * as fs from 'node:fs'
const contents = fs.readFileSync('file path', 'utf8')
console.log(contents)
/////////////////////////////////////////////////
import * as os from 'node:os';
os.EOL
import * as util from 'node:util';
const INSPECT_OPTIONS = {
depth: Infinity,
colors: true,
maxArrayLength: Infinity,
//breakLength: getꓽterminal_size().columns,
//compact: true,
}
function dump(...args: any[]) {
if (args.length === 1) {
console.log(util.inspect(args[0], INSPECT_OPTIONS))
}
else {
console.log(util.inspect(args, INSPECT_OPTIONS))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment