repl.it
@ulisesantana/

Validate

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const assert = require('assert');

const example = {
  a: 24,
  c: {
    meh: false,
    answer: {
      realAnswer: 42
    }
  }
}

const emptyExample = {};

const isNotNull = (obj, path = '') => {
  if (path.length > 0 && obj !== null) {
    const [h, ...t] = path.split('.');
    return obj.hasOwnProperty(h)
      ? isNotNull(obj[h], t.join('.'))
      : false;
  } else {
    return obj !== null;
  } 
}

assert.strictEqual(false, isNotNull(null));
assert.strictEqual(false, isNotNull(emptyExample, 'a'));
assert.strictEqual(true, isNotNull(example, 'a'));
assert.strictEqual(true, isNotNull(example, 'c.answer'));
assert.strictEqual(true, isNotNull(example, 'c.meh'));
assert.strictEqual(false, isNotNull(example, 'c.whatever'));
assert.strictEqual(true, isNotNull(example, 'c.answer.realAnswer'));
assert.strictEqual(false, isNotNull(example, 'c.whatever.realAnswer'));

console.log('End of assertions')

Fetching token
?