Compare commits

...

5 Commits

Author SHA1 Message Date
cbee6a4509 Timezone abbreviation support
All checks were successful
Build / Publish Docs (push) Successful in 1m48s
Build / Build NPM Project (push) Successful in 2m48s
Build / Tag Version (push) Successful in 10s
2026-04-11 16:29:53 -04:00
e8f81bb584 Proper xml to json format
All checks were successful
Build / Publish Docs (push) Successful in 49s
Build / Build NPM Project (push) Successful in 56s
Build / Tag Version (push) Successful in 10s
2026-04-04 18:32:48 -04:00
4179b4010a Revert "added fast-xml-parser for testing"
Some checks failed
Build / Tag Version (push) Has been cancelled
Build / Build NPM Project (push) Has been cancelled
Build / Publish Docs (push) Has been cancelled
This reverts commit 15ac52b6a0.
2026-04-04 18:20:44 -04:00
15ac52b6a0 added fast-xml-parser for testing
Some checks failed
Build / Publish Docs (push) Failing after 16s
Build / Build NPM Project (push) Failing after 54s
Build / Tag Version (push) Has been skipped
2026-04-04 18:18:24 -04:00
c778f3d280 Dependency updates, bump to 0.29.0
All checks were successful
Build / Publish Docs (push) Successful in 59s
Build / Build NPM Project (push) Successful in 1m6s
Build / Tag Version (push) Successful in 13s
2026-04-04 17:05:17 -04:00
4 changed files with 189 additions and 64 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.28.17", "version": "0.29.2",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@@ -78,19 +78,62 @@ export function dayOfYear(date: Date): number {
* *
* @param {string} format How date string will be formatted, default: `YYYY-MM-DD H:mm A` * @param {string} format How date string will be formatted, default: `YYYY-MM-DD H:mm A`
* @param {Date | number | string} date Date or timestamp, defaults to now * @param {Date | number | string} date Date or timestamp, defaults to now
* @param tz Set timezone offset * @param tz Set timezone offset in: hours (-4) or minutes (430) or IANA string (America/New_York)
* @return {string} Formated date * @return {string} Formated date
*/ */
export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | number | string = new Date(), tz: string | number = 'local'): string { export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | number | string = new Date(), tz: string | number = 'local'): string {
if (typeof date === 'number' || typeof date === 'string') date = new Date(date); if (typeof date === 'number' || typeof date === 'string') date = new Date(date);
if (isNaN(date.getTime())) throw new Error('Invalid date input'); if (isNaN(date.getTime())) throw new Error('Invalid date input');
const numericTz = typeof tz === 'number';
const localTz = tz === 'local' || (!numericTz && tz.toLowerCase?.() === 'local'); const TIMEZONE_MAP = [
const tzName = localTz ? Intl.DateTimeFormat().resolvedOptions().timeZone : numericTz ? 'UTC' : tz; { name: 'IDLW', iana: 'Etc/GMT+12', offset: -720 },
{ name: 'SST', iana: 'Pacific/Pago_Pago', offset: -660 },
{ name: 'HST', iana: 'Pacific/Honolulu', offset: -600 },
{ name: 'AKST', iana: 'America/Anchorage', offset: -540 },
{ name: 'PST', iana: 'America/Los_Angeles', offset: -480 },
{ name: 'MST', iana: 'America/Denver', offset: -420 },
{ name: 'CST', iana: 'America/Chicago', offset: -360 },
{ name: 'EST', iana: 'America/New_York', offset: -300 },
{ name: 'AST', iana: 'America/Halifax', offset: -240 },
{ name: 'BRT', iana: 'America/Sao_Paulo', offset: -180 },
{ name: 'MAT', iana: 'Atlantic/South_Georgia', offset: -120 },
{ name: 'AZOT', iana: 'Atlantic/Azores', offset: -60 },
{ name: 'UTC', iana: 'UTC', offset: 0 },
{ name: 'CET', iana: 'Europe/Paris', offset: 60 },
{ name: 'EET', iana: 'Europe/Athens', offset: 120 },
{ name: 'MSK', iana: 'Europe/Moscow', offset: 180 },
{ name: 'GST', iana: 'Asia/Dubai', offset: 240 },
{ name: 'PKT', iana: 'Asia/Karachi', offset: 300 },
{ name: 'IST', iana: 'Asia/Kolkata', offset: 330 },
{ name: 'BST', iana: 'Asia/Dhaka', offset: 360 },
{ name: 'ICT', iana: 'Asia/Bangkok', offset: 420 },
{ name: 'CST', iana: 'Asia/Shanghai', offset: 480 },
{ name: 'JST', iana: 'Asia/Tokyo', offset: 540 },
{ name: 'AEST', iana: 'Australia/Sydney', offset: 600 },
{ name: 'SBT', iana: 'Pacific/Guadalcanal', offset: 660 },
{ name: 'TOT', iana: 'Pacific/Tongatapu', offset: 780 },
{ name: 'LINT', iana: 'Pacific/Kiritimati', offset: 840 },
];
let numericTz = typeof tz === 'number';
const localTz = tz === 'local' || (!numericTz && tz.toString().toLowerCase?.() === 'local');
let tzName = localTz ? Intl.DateTimeFormat().resolvedOptions().timeZone : numericTz ? 'UTC' : tz;
let offsetMinutes = 0;
if (numericTz) {
// Convert hours to minutes if offset is small (likely hours)
offsetMinutes = Math.abs(tz as number) < 24 ? (tz as number) * 60 : (tz as number);
// Find closest matching timezone
const closest = TIMEZONE_MAP.reduce((prev, curr) =>
Math.abs(curr.offset - offsetMinutes) < Math.abs(prev.offset - offsetMinutes) ? curr : prev
);
tzName = closest.iana;
}
if (!numericTz && tzName !== 'UTC') { if (!numericTz && tzName !== 'UTC') {
try { try {
new Intl.DateTimeFormat('en-US', { timeZone: tzName }).format(); new Intl.DateTimeFormat('en-US', { timeZone: <string>tzName }).format();
} catch { } catch {
throw new Error(`Invalid timezone: ${tzName}`); throw new Error(`Invalid timezone: ${tzName}`);
} }
@@ -99,9 +142,10 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
let zonedDate = new Date(date); let zonedDate = new Date(date);
let get: (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds') => number; let get: (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds') => number;
const partsMap: Record<string, string> = {}; const partsMap: Record<string, string> = {};
if (!numericTz && tzName !== 'UTC') { if (!numericTz && tzName !== 'UTC') {
const parts = new Intl.DateTimeFormat('en-US', { const parts = new Intl.DateTimeFormat('en-US', {
timeZone: tzName, timeZone: <string>tzName,
year: 'numeric', month: '2-digit', day: '2-digit', weekday: 'long', year: 'numeric', month: '2-digit', day: '2-digit', weekday: 'long',
hour: '2-digit', minute: '2-digit', second: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit',
hour12: false hour12: false
@@ -127,8 +171,7 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
} }
}; };
} else { } else {
const offset = numericTz ? tz as number : 0; zonedDate = new Date(date.getTime() + offsetMinutes * 60 * 1000);
zonedDate = new Date(date.getTime() + offset * 60 * 60 * 1000);
get = (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds'): number => zonedDate[`getUTC${fn}`](); get = (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds'): number => zonedDate[`getUTC${fn}`]();
} }
@@ -140,13 +183,12 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
function getTZOffset(): string { function getTZOffset(): string {
if(numericTz) { if(numericTz) {
const total = (tz as number) * 60; const hours = Math.floor(Math.abs(offsetMinutes) / 60);
const hours = Math.floor(Math.abs(total) / 60); const mins = Math.abs(offsetMinutes) % 60;
const mins = Math.abs(total) % 60; return `${offsetMinutes >= 0 ? '+' : '-'}${String(hours).padStart(2, '0')}:${String(mins).padStart(2, '0')}`;
return `${tz >= 0 ? '+' : '-'}${String(hours).padStart(2, '0')}:${String(mins).padStart(2, '0')}`;
} }
try { try {
const offset = new Intl.DateTimeFormat('en-US', {timeZone: tzName, timeZoneName: 'longOffset', hour: '2-digit', minute: '2-digit',}) const offset = new Intl.DateTimeFormat('en-US', {timeZone: <string>tzName, timeZoneName: 'longOffset', hour: '2-digit', minute: '2-digit',})
.formatToParts(<Date>date).find(p => p.type === 'timeZoneName')?.value.match(/([+-]\d{2}:\d{2})/)?.[1]; .formatToParts(<Date>date).find(p => p.type === 'timeZoneName')?.value.match(/([+-]\d{2}:\d{2})/)?.[1];
if (offset) return offset; if (offset) return offset;
} catch {} } catch {}
@@ -154,12 +196,11 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
} }
function getTZAbbr(): string { function getTZAbbr(): string {
if (numericTz && tz === 0) return 'UTC';
try { try {
return new Intl.DateTimeFormat('en-US', { timeZone: tzName, timeZoneName: 'short' }) return new Intl.DateTimeFormat('en-US', { timeZone: <string>tzName, timeZoneName: 'short' })
.formatToParts(<Date>date).find(p => p.type === 'timeZoneName')?.value || ''; .formatToParts(<Date>date).find(p => p.type === 'timeZoneName')?.value || '';
} catch { } catch {
return tzName; return <string>tzName;
} }
} }

View File

@@ -3,6 +3,11 @@
* @param {string} xml - The XML string to parse * @param {string} xml - The XML string to parse
* @returns {Object} An object with `tag`, `attributes`, and `children` properties * @returns {Object} An object with `tag`, `attributes`, and `children` properties
*/ */
/**
* Parses an XML string into a structured JavaScript object (fast-xml-parser format).
* @param {string} xml - The XML string to parse
* @returns {Object} An object with tag names as keys and text content or nested objects as values
*/
export function fromXml(xml: string) { export function fromXml(xml: string) {
xml = xml.trim(); xml = xml.trim();
let pos = 0; let pos = 0;
@@ -13,8 +18,8 @@ export function fromXml(xml: string) {
pos++; // skip < pos++; // skip <
if(xml[pos] === '?') { if(xml[pos] === '?') {
parseDeclaration(); const declaration = parseDeclaration();
return parseNode(); return { ['?' + declaration]: '', ...parseNode() };
} }
if(xml[pos] === '!') { if(xml[pos] === '!') {
@@ -28,11 +33,13 @@ export function fromXml(xml: string) {
if(xml[pos] === '/' && xml[pos + 1] === '>') { if(xml[pos] === '/' && xml[pos + 1] === '>') {
pos += 2; // skip /> pos += 2; // skip />
return { tag: tagName, attributes, children: [] }; return { [tagName]: '' };
} }
pos++; // skip > pos++; // skip >
const children = []; const children: any[] = [];
let textContent = '';
while(pos < xml.length) { while(pos < xml.length) {
skipWhitespace(); skipWhitespace();
if(xml[pos] === '<' && xml[pos + 1] === '/') { if(xml[pos] === '<' && xml[pos + 1] === '/') {
@@ -42,20 +49,51 @@ export function fromXml(xml: string) {
pos++; // skip > pos++; // skip >
break; break;
} }
const startPos = pos;
const child = parseNode(); const child = parseNode();
if(child) children.push(child); if(typeof child === 'string') {
textContent += child;
} else if(child) {
children.push(child);
} }
return { tag: tagName, attributes, children };
} }
/** Parses and returns the tag name at the current position */ // If only text content, return simple value
if(children.length === 0 && textContent) {
const value = isNumeric(textContent) ? Number(textContent) : textContent;
return { [tagName]: value };
}
// If only text with no children
if(children.length === 0) {
return { [tagName]: '' };
}
// Merge children into object
const result: any = {};
for(const child of children) {
for(const [key, value] of Object.entries(child)) {
if(result[key]) {
// Convert to array if duplicate tags
if(!Array.isArray(result[key])) {
result[key] = [result[key]];
}
result[key].push(value);
} else {
result[key] = value;
}
}
}
return { [tagName]: result };
}
function parseTagName() { function parseTagName() {
let name = ''; let name = '';
while (pos < xml.length && /[a-zA-Z0-9_:-]/.test(xml[pos])) name += xml[pos++]; while (pos < xml.length && /[a-zA-Z0-9_:-]/.test(xml[pos])) name += xml[pos++];
return name; return name;
} }
/** Parses and returns an object containing all attributes at the current position */
function parseAttributes() { function parseAttributes() {
const attrs: any = {}; const attrs: any = {};
while (pos < xml.length) { while (pos < xml.length) {
@@ -76,7 +114,6 @@ export function fromXml(xml: string) {
return attrs; return attrs;
} }
/** Parses and returns text content, or null if empty */
function parseText() { function parseText() {
let text = ''; let text = '';
while (pos < xml.length && xml[pos] !== '<') text += xml[pos++]; while (pos < xml.length && xml[pos] !== '<') text += xml[pos++];
@@ -84,23 +121,30 @@ export function fromXml(xml: string) {
return text ? escapeXml(text, true) : null; return text ? escapeXml(text, true) : null;
} }
/** Skips over XML declaration (<?xml ... ?>) */
function parseDeclaration() { function parseDeclaration() {
pos++; // skip ?
let name = '';
while (pos < xml.length && xml[pos] !== ' ' && xml[pos] !== '?') {
name += xml[pos++];
}
while (xml[pos] !== '>') pos++; while (xml[pos] !== '>') pos++;
pos++; pos++;
return name;
} }
/** Skips over XML comments (<!-- ... -->) */
function parseComment() { function parseComment() {
while (!(xml[pos] === '-' && xml[pos + 1] === '-' && xml[pos + 2] === '>')) pos++; while (!(xml[pos] === '-' && xml[pos + 1] === '-' && xml[pos + 2] === '>')) pos++;
pos += 3; pos += 3;
} }
/** Advances position past any whitespace characters */
function skipWhitespace() { function skipWhitespace() {
while (pos < xml.length && /\s/.test(xml[pos])) pos++; while (pos < xml.length && /\s/.test(xml[pos])) pos++;
} }
function isNumeric(str: string) {
return !isNaN(Number(str)) && !isNaN(parseFloat(str)) && str.trim() !== '';
}
return parseNode(); return parseNode();
} }

View File

@@ -5,71 +5,80 @@ describe('XML Parser', () => {
it('should parse simple tag', () => { it('should parse simple tag', () => {
const xml = '<root></root>'; const xml = '<root></root>';
const result = fromXml(xml); const result = fromXml(xml);
expect(result).toEqual({ tag: 'root', attributes: {}, children: [] }); expect(result).toEqual({ root: '' });
}); });
it('should parse self-closing tag', () => { it('should parse self-closing tag', () => {
const xml = '<item />'; const xml = '<item />';
const result = fromXml(xml); const result = fromXml(xml);
expect(result).toEqual({ tag: 'item', attributes: {}, children: [] }); expect(result).toEqual({ item: '' });
}); });
it('should parse tag with attributes', () => { it('should parse tag with attributes (ignored in fast-xml-parser format)', () => {
const xml = '<user id="1" name="someone" />'; const xml = '<user id="1" name="someone" />';
const result = fromXml(xml); const result = fromXml(xml);
expect(result).toEqual({ expect(result).toEqual({ user: '' });
tag: 'user',
attributes: { id: '1', name: 'someone' },
children: []
});
}); });
it('should parse tag with text content', () => { it('should parse tag with text content', () => {
const xml = '<email>someone@example.com</email>'; const xml = '<email>someone@example.com</email>';
const result = fromXml(xml); const result = fromXml(xml);
expect(result).toEqual({ expect(result).toEqual({ email: 'someone@example.com' });
tag: 'email',
attributes: {},
children: ['someone@example.com']
}); });
it('should parse tag with numeric content', () => {
const xml = '<ttl>240</ttl>';
const result = fromXml(xml);
expect(result).toEqual({ ttl: 240 });
}); });
it('should parse nested tags', () => { it('should parse nested tags', () => {
const xml = '<root><child>text</child></root>'; const xml = '<root><child>text</child></root>';
const result = fromXml(xml); const result = fromXml(xml);
expect(result).toEqual({ expect(result).toEqual({
tag: 'root', root: {
attributes: {}, child: 'text'
children: [ }
{ tag: 'child', attributes: {}, children: ['text'] }
]
}); });
}); });
it('should parse multiple children', () => { it('should parse multiple children with same tag as array', () => {
const xml = '<root><a /><b /><c /></root>'; const xml = '<root><item>a</item><item>b</item><item>c</item></root>';
const result = fromXml(xml); const result = fromXml(xml);
expect(result.children.length).toBe(3); expect(result).toEqual({
expect(result.children[0]).toEqual({ tag: 'a', attributes: {}, children: [] }); root: {
item: ['a', 'b', 'c']
}
});
}); });
it('should skip XML declaration', () => { it('should parse mixed children', () => {
const xml = '<root><a>1</a><b>2</b><c>3</c></root>';
const result = fromXml(xml);
expect(result.root).toEqual({ a: 1, b: 2, c: 3 });
});
it('should skip XML declaration and include as key', () => {
const xml = '<?xml version="1.0"?><root />'; const xml = '<?xml version="1.0"?><root />';
const result = fromXml(xml); const result = fromXml(xml);
expect(result.tag).toBe('root'); expect(result).toHaveProperty('?xml');
expect(result).toHaveProperty('root');
}); });
it('should skip comments', () => { it('should skip comments', () => {
const xml = '<root><!-- comment --><child /></root>'; const xml = '<root><!-- comment --><child>text</child></root>';
const result = fromXml(xml); const result = fromXml(xml);
expect(result.children.length).toBe(1); expect(result).toEqual({
expect(result.children[0].tag).toBe('child'); root: {
child: 'text'
}
});
}); });
it('should handle escaped characters', () => { it('should handle escaped characters', () => {
const xml = '<text>&lt;hello&gt; &amp; &quot;world&quot;</text>'; const xml = '<text>&lt;hello&gt; &amp; &quot;world&quot;</text>';
const result = fromXml(xml); const result = fromXml(xml);
expect(result.children[0]).toBe('<hello> & "world"'); expect(result.text).toBe('<hello> & "world"');
}); });
it('should parse complex nested structure', () => { it('should parse complex nested structure', () => {
@@ -82,10 +91,37 @@ describe('XML Parser', () => {
</root> </root>
`; `;
const result = fromXml(xml); const result = fromXml(xml);
expect(result.tag).toBe('root'); expect(result).toEqual({
expect(result.children[0].tag).toBe('user'); root: {
expect(result.children[0].attributes.name).toBe('someone'); user: {
expect(result.children[0].children.length).toBe(2); email: 'someone@example.com',
active: ''
}
}
});
});
it('should parse RSS-like structure with multiple items', () => {
const xml = `
<rss>
<channel>
<title>Test Feed</title>
<item>
<title>Item 1</title>
<link>http://example.com/1</link>
</item>
<item>
<title>Item 2</title>
<link>http://example.com/2</link>
</item>
</channel>
</rss>
`;
const result = fromXml(xml);
expect(result.rss.channel.title).toBe('Test Feed');
expect(Array.isArray(result.rss.channel.item)).toBe(true);
expect(result.rss.channel.item.length).toBe(2);
expect(result.rss.channel.item[0].title).toBe('Item 1');
}); });
}); });
@@ -154,7 +190,7 @@ describe('XML Parser', () => {
}); });
describe('round-trip', () => { describe('round-trip', () => {
it('should encode and decode to same structure', () => { it('should parse toXml output back to fast-xml-parser format', () => {
const obj = { const obj = {
tag: 'root', tag: 'root',
attributes: { id: '1' }, attributes: { id: '1' },
@@ -164,7 +200,11 @@ describe('XML Parser', () => {
}; };
const xml = toXml(obj); const xml = toXml(obj);
const parsed = fromXml(xml); const parsed = fromXml(xml);
expect(parsed).toEqual(obj); expect(parsed).toEqual({
root: {
child: 'text'
}
});
}); });
}); });
}); });