Skip to content

Commit

Permalink
fixed bug with parseDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
ppaska committed Mar 10, 2024
1 parent ab3d1cd commit a3722ae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 115 deletions.
122 changes: 9 additions & 113 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,123 +14,19 @@ <h4>Data Pipe testing page. Do not expect anything here! </h4>
<input id="input" type="file" onchange="fileChange(this.files[0])" />

<script>
let result = null;
let firstChar = '';

function readContent(file) {
const fr = new FileReader();
const promise = new Promise((success, fail) => {
fr.onload = e => success(e.target?.result);
fr.onerror = e => fail('Error loading file');
});
fr.readAsText(file);

return promise;

}
async function fileChange(file) {
//file
if (!file) {
return;
}

if (file) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
const data = JSON.parse(evt.target.result);
console.log('* fieldsInfo => ', dp.dataPipe(data).getFieldsInfo(), data);
}
reader.onerror = function (evt) {
console.log("error reading file");
}
}
}

function iterateChunks(file, callback) {
const chunkSize = 1024; // 10 * 64 * 64 * 1024;
let offset = 0;
const decoder = new TextDecoder();
const fr = new FileReader();
fr.onload = () => {
const view = new Uint8Array(fr.result);
const text = decoder.decode(view);
callback(text);

offset += chunkSize;
seek();
}
fr.onerror = (err) => {
console.log(err)
};

seek();

function seek() {
if (offset >= file.size) {
console.log('==> done ==>', result)

return;
}

const slice = file.slice(offset, offset + chunkSize);
fr.readAsArrayBuffer(slice)
}
}

function jsonProcessor(text) {
const result = [];
const parser = new dp.dataPipe().JSONParser();
for (const ch of text) {
parser.processJsonItems(ch, (itemText, key) => {
let item = JSON.parse(itemText);

if (typeof key === 'string') {
item = { key, item };
}

result.push(item);
});
}

return result;
}

function onButtonClick() {
const data = [{ a: null, d:{d1:22, d2:33} }, { b: 2, d:{d1:221, d2:331, d3: null} }];
console.log('* fieldsInfo => ', dp.dataPipe(data).flattenObject());
// const json = `[{"value":"test value\\\\d"}]`;
// console.log(json)
// console.log(JSON.parse(json)[0].value)
// console.log(jsonProcessor(json))
// const iterator = dp.dataPipe()
// .jsonParseIterator('{v:{vi:test}, arr:{vi:test2}}');

// for (let v of iterator) {
// console.log(v);
// }
// console.log(Array.from(iterator));
return;

// const mapFn = r => ({ val1: r });
// const fdFn = (arr) => dp.dataPipe(arr.map(mapFn)).getFieldsInfo();

// const data = fdFn([new Date(2001, 1, 1), new Date()]);

const array = ['2021-05-01', null, '2021-05-01'];
data = dp
.dataPipe(array)
.getFieldsInfo();

console.log('==>', dp.dataPipe().rtrim('net.', '.'))

// fetch("https://restcountries.eu/rest/v2/regionalbloc/eu")
// .then(r => {
// r.json().then(items => {
// console.log('==>', items, dp.dataPipe(items).getFieldsInfo());
// });
// })
const data = [
{
"dt2": "2020-02-21T13:49:15.167Z",
"dt": "20-Aug-2019",
"dt1": "06-Aug-2019",
"value": "8=FIX.4.4^9=58^35=0^49=BuySide^56=SellSide^34=3^52=20190605-12:29:20.259^10=172^"
}
]

console.log('* fieldsInfo => ', dp.dataPipe(data).getFieldsInfo());
}

onButtonClick();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datapipe-js",
"version": "0.3.30",
"version": "0.3.31",
"description": "dataPipe is a data processing and data analytics library for JavaScript. Inspired by LINQ (C#) and Pandas (Python)",
"main": "dist/cjs/data-pipe.js",
"module": "dist/esm/data-pipe.mjs",
Expand Down
8 changes: 8 additions & 0 deletions src/tests/utils-pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('Test dataUtils', () => {
expect(dateToString(parseDatetimeOrNull(strDate) as Date)).toBe(strDate);
});

it('parseDateTime 1', () => {
const dt = parseDatetimeOrNull('06-Aug-2020');
expect(dt).toBeInstanceOf(Date);

const dt2 = parseDatetimeOrNull('8=FIX.4.4^9=58^35=0^49=BuySide^56=SellSide^34=3^52=20190605-12:29:20.259^10=172^');
expect(dt2).toBeNull();
});

it('parseDateTime with larger miliseconds', () => {
const dt = parseDatetimeOrNull('2020-06-08T13:49:15.16789');
expect(dt).toBeInstanceOf(Date);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ export function parseDatetimeOrNull(

const strTokens = strValue
.replace('T', ' ')
.replace('Z', ' ')
.replace('.', ' ')
.toLowerCase()
.split(/[: /-]/);
const dt = strTokens.map(parseFloat);
const dt = strTokens.map(s => parseNumberOrNull(s) ?? Number.NaN);

let d: Date | null = null;

Expand Down

0 comments on commit a3722ae

Please sign in to comment.