1

I would like to be able to check TOML files for undefined or empty strings. So in the below example 0 and false should have returned that they have been set, where test4 is clearly not defined, but the checks from says they are, which is wrong.

test.js

const TOML = require('@iarna/toml')
const fs = require('fs');

const f = TOML.parse(fs.readFileSync('test.toml', 'utf-8'))
console.log(f)

function isEmpty(str) {return (!str || 0 === str.length)}
function isBlank(str) {return (!str || /^\s*$/.test(str))}

console.log(isBlank(f['test1']))
console.log(isBlank(f['test2']))
console.log(isBlank(f['test3']))
console.log(isBlank(f['test4']))

console.log(isEmpty(f['test1']))
console.log(isEmpty(f['test2']))
console.log(isEmpty(f['test3']))
console.log(isEmpty(f['test4']))

test.toml

test1 = 0
test2 = false
test3 = ''

which gives

{ test1: 0, test2: false, test3: '' }
true
true
true
true
true
true
true
true

I am guessing the problem is related to the TOML parser is being clever, as it understands 0 is an integer and false is a boolean.

Question

How can I make a test function isNotSet() that for the given TOML file will return?

false
false
true
true

or true true false false

depending on your take in the problem.

Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162

1 Answers1

1
function isSet(str) {return (str === undefined || str === null || str === '')}

which gives

false
false
true
true

where you can extend it with extra checks as needed.

d2xdt2
  • 370
  • 3
  • 8