-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved old tests to a separate directory + created the first modern test.
- Loading branch information
Showing
28 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
function Counter() { | ||
this.objects = 0; | ||
this.keys = 0; | ||
this.arrays = 0; | ||
this.nulls = 0; | ||
this.trues = 0; | ||
this.falses = 0; | ||
this.numbers = 0; | ||
this.strings = 0; | ||
} | ||
|
||
Counter.walk = function walk(o, counter) { | ||
switch (typeof o) { | ||
case 'string': | ||
++counter.strings; | ||
return; | ||
case 'number': | ||
++counter.numbers; | ||
return; | ||
case 'boolean': | ||
if (o) { | ||
++counter.trues; | ||
} else { | ||
++counter.falses; | ||
} | ||
return; | ||
} | ||
if (o === null) { | ||
++counter.nulls; | ||
return; | ||
} | ||
if (o instanceof Array) { | ||
++counter.arrays; | ||
o.forEach(function(o) { | ||
walk(o, counter); | ||
}); | ||
return; | ||
} | ||
++counter.objects; | ||
for (let key in o) { | ||
if (o.hasOwnProperty(key)) { | ||
++counter.keys; | ||
walk(o[key], counter); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = {Counter}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
const {Readable} = require('node:stream'); | ||
|
||
const readString = (string, quant, options) => new Readable(Object.assign({objectMode: true}, options, { | ||
read() { | ||
if (isNaN(quant)) { | ||
this.push(string, 'utf8'); | ||
} else if (string instanceof Buffer) { | ||
for (let i = 0; i < string.length; i += quant) { | ||
this.push(string.subarray(i, i + quant)); | ||
} | ||
} else { | ||
for (let i = 0; i < string.length; i += quant) { | ||
this.push(string.substr(i, quant), 'utf8'); | ||
} | ||
} | ||
this.push(null); | ||
} | ||
})); | ||
|
||
|
||
module.exports = readString; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
import test from 'tape-six'; | ||
import chain from 'stream-chain'; | ||
|
||
import parser from '../src/parser.js'; | ||
|
||
import readString from './read-string.js'; | ||
|
||
test.asPromise('smoke test', (t, resolve, reject) => { | ||
const input = '{"a": 1, "b": true, "c": ["d"]}', | ||
pipeline = chain([readString(input), parser({packValues: false})]), | ||
result = []; | ||
|
||
pipeline.on('data', chunk => result.push({name: chunk.name, val: chunk.value})); | ||
pipeline.on('error', () => reject()); | ||
pipeline.on('end', () => { | ||
t.ok(result.length === 20); | ||
t.ok(result[0].name === 'startObject'); | ||
t.ok(result[1].name === 'startKey'); | ||
t.ok(result[2].name === 'stringChunk' && result[2].val === 'a'); | ||
t.ok(result[3].name === 'endKey'); | ||
t.ok(result[4].name === 'startNumber'); | ||
t.ok(result[5].name === 'numberChunk' && result[5].val === '1'); | ||
t.ok(result[6].name === 'endNumber'); | ||
t.ok(result[7].name === 'startKey'); | ||
t.ok(result[8].name === 'stringChunk' && result[8].val === 'b'); | ||
t.ok(result[9].name === 'endKey'); | ||
t.ok(result[10].name === 'trueValue' && result[10].val === true); | ||
t.ok(result[11].name === 'startKey'); | ||
t.ok(result[12].name === 'stringChunk' && result[12].val === 'c'); | ||
t.ok(result[13].name === 'endKey'); | ||
t.ok(result[14].name === 'startArray'); | ||
t.ok(result[15].name === 'startString'); | ||
t.ok(result[16].name === 'stringChunk' && result[16].val === 'd'); | ||
t.ok(result[17].name === 'endString'); | ||
t.ok(result[18].name === 'endArray'); | ||
t.ok(result[19].name === 'endObject'); | ||
resolve(); | ||
}); | ||
}); |