Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 28716c7b5a | |||
| d530f6abdf | |||
| cbee6a4509 | |||
| e8f81bb584 | |||
| 4179b4010a | |||
| 15ac52b6a0 | |||
| c778f3d280 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.28.17",
|
"version": "0.29.4",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
@@ -241,7 +241,6 @@ export function snakeCase(str?: string): string {
|
|||||||
return wordSegments(str).map(w => w.toLowerCase()).join("_");
|
return wordSegments(str).map(w => w.toLowerCase()).join("_");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splice a string together (Similar to Array.splice)
|
* Splice a string together (Similar to Array.splice)
|
||||||
*
|
*
|
||||||
@@ -257,6 +256,20 @@ export function strSplice(str: string, start: number, deleteCount: number, inser
|
|||||||
return before + insert + after;
|
return before + insert + after;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function titleCase(str: string) {
|
||||||
|
// Normalize separators: replace underscores and hyphens with spaces
|
||||||
|
let normalizedStr = str.replace(/(_|-)/g, ' ');
|
||||||
|
// Handle CamelCase/PascalCase boundaries: insert a space before capital letters
|
||||||
|
normalizedStr = normalizedStr.replace(/([a-z])([A-Z])/g, '$1 $2');
|
||||||
|
// Lowercase the whole string, split by any whitespace, and capitalize each word
|
||||||
|
let words = normalizedStr.toLowerCase().split(/\s+/).filter(Boolean);
|
||||||
|
const titledWords = words.map(word => {
|
||||||
|
if (word.length === 0) return '';
|
||||||
|
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
||||||
|
});
|
||||||
|
return titledWords.join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all substrings that match a given pattern.
|
* Find all substrings that match a given pattern.
|
||||||
*
|
*
|
||||||
|
|||||||
77
src/time.ts
77
src/time.ts
@@ -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
|
||||||
@@ -111,7 +155,7 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
|
|||||||
});
|
});
|
||||||
|
|
||||||
const monthValue = parseInt(partsMap.month) - 1;
|
const monthValue = parseInt(partsMap.month) - 1;
|
||||||
const dayOfWeekValue = new Date(`${partsMap.year}-${partsMap.month}-${partsMap.day}`).getDay();
|
const dayOfWeekValue = new Date(Date.UTC(parseInt(partsMap.year), parseInt(partsMap.month) - 1, parseInt(partsMap.day))).getUTCDay();
|
||||||
const hourValue = parseInt(partsMap.hour);
|
const hourValue = parseInt(partsMap.hour);
|
||||||
|
|
||||||
get = (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds'): number => {
|
get = (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds'): number => {
|
||||||
@@ -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}`]();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,14 +182,13 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
68
src/xml.ts
68
src/xml.ts
@@ -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 };
|
|
||||||
|
// 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 };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Parses and returns the tag name at the current position */
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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><hello> & "world"</text>';
|
const xml = '<text><hello> & "world"</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'
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user