Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c60f52b5e | |||
| 35c14d01c2 | |||
| 92f36fdb5b | |||
| 721fe2dcf8 | |||
| 4dfd70f6a7 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.30.5",
|
"version": "0.30.8",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
10
src/ip.ts
10
src/ip.ts
@@ -45,18 +45,18 @@ export function reservedIp(ip: string): boolean {
|
|||||||
/**
|
/**
|
||||||
* Check if IP is within a reserved range
|
* Check if IP is within a reserved range
|
||||||
* @param {string} ip
|
* @param {string} ip
|
||||||
* @returns {string | null}
|
* @returns {null | 'Invalid' | 'Host' | 'Loopback' | 'Private (Class A)' | 'Private (Class B)' | 'Link-Local' | 'IETF Protocol Assignments' | 'Documentation (TEST-NET-1)' | 'Benchmarking' | 'Documentation (TEST-NET-2)' | 'Documentation (TEST-NET-3)' | 'Multicast' | 'Reserved (Class E)'} null if public, class name if reserved
|
||||||
*/
|
*/
|
||||||
export function isReserved(ip: string) {
|
export function isReserved(ip: string): null | 'Invalid' | 'Host' | 'Loopback' | 'Private (Class A)' | 'Private (Class B)' | 'Link-Local' | 'IETF Protocol Assignments' | 'Documentation (TEST-NET-1)' | 'Benchmarking' | 'Documentation (TEST-NET-2)' | 'Documentation (TEST-NET-3)' | 'Multicast' | 'Reserved (Class E)' {
|
||||||
const ipToNumber = (ip: string) => {
|
const ipToNumber = (ip: string) => {
|
||||||
return ip.split('.').reduce((acc: number, octet: string) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
|
return ip.split('.').reduce((acc: number, octet: string) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ipNum = ipToNumber(ip);
|
const ipNum = ipToNumber(ip);
|
||||||
if (ipNum === null) return 'invalid';
|
if (ipNum === null) return 'Invalid';
|
||||||
|
|
||||||
const reserved = [
|
const reserved = [
|
||||||
{ name: "This host", start: ipToNumber("0.0.0.0"), end: ipToNumber("0.255.255.255") },
|
{ name: "Host", start: ipToNumber("0.0.0.0"), end: ipToNumber("0.255.255.255") },
|
||||||
{ name: "Loopback", start: ipToNumber("127.0.0.0"), end: ipToNumber("127.255.255.255") },
|
{ name: "Loopback", start: ipToNumber("127.0.0.0"), end: ipToNumber("127.255.255.255") },
|
||||||
{ name: "Private (Class A)", start: ipToNumber("10.0.0.0"), end: ipToNumber("10.255.255.255") },
|
{ name: "Private (Class A)", start: ipToNumber("10.0.0.0"), end: ipToNumber("10.255.255.255") },
|
||||||
{ name: "CGNAT", start: ipToNumber("100.64.0.0"), end: ipToNumber("100.127.255.255") },
|
{ name: "CGNAT", start: ipToNumber("100.64.0.0"), end: ipToNumber("100.127.255.255") },
|
||||||
@@ -73,5 +73,5 @@ export function isReserved(ip: string) {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const match = reserved.find(r => ipNum >= r.start && ipNum <= r.end);
|
const match = reserved.find(r => ipNum >= r.start && ipNum <= r.end);
|
||||||
return match ? match.name : null;
|
return match ? <any>match.name : null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,8 +97,10 @@ export function deepCopy<T>(value: T): T {
|
|||||||
* @return {any} The des
|
* @return {any} The des
|
||||||
*/
|
*/
|
||||||
export function deepMerge<T>(target: any, ...sources: any[]): T {
|
export function deepMerge<T>(target: any, ...sources: any[]): T {
|
||||||
|
const BLOCKED = new Set(['__proto__', 'constructor', 'prototype']);
|
||||||
sources.forEach(s => {
|
sources.forEach(s => {
|
||||||
for(const key in s) {
|
for(const key in s) {
|
||||||
|
if(BLOCKED.has(key) || !Object.prototype.hasOwnProperty.call(s, key)) continue;
|
||||||
if(s[key] && typeof s[key] == 'object' && !Array.isArray(s[key])) {
|
if(s[key] && typeof s[key] == 'object' && !Array.isArray(s[key])) {
|
||||||
if(!target[key]) target[key] = {};
|
if(!target[key]) target[key] = {};
|
||||||
deepMerge(target[key], s[key]);
|
deepMerge(target[key], s[key]);
|
||||||
|
|||||||
26
src/xml.ts
26
src/xml.ts
@@ -1,12 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* Parses an XML string into a structured JavaScript object.
|
* Parses an XML string into a plain JavaScript object.
|
||||||
|
* Each tag becomes a key. Attributes and child tags are merged as
|
||||||
|
* sibling properties on that tag's object. Duplicate tag/attribute
|
||||||
|
* names are collapsed into arrays. Text-only tags resolve to their
|
||||||
|
* (optionally numeric) value; if a tag has both text and attributes/
|
||||||
|
* children, the text is kept under a `_text` key.
|
||||||
* @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} The parsed object tree
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* 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();
|
||||||
@@ -33,11 +33,11 @@ export function fromXml(xml: string) {
|
|||||||
|
|
||||||
if(xml[pos] === '/' && xml[pos + 1] === '>') {
|
if(xml[pos] === '/' && xml[pos + 1] === '>') {
|
||||||
pos += 2; // skip />
|
pos += 2; // skip />
|
||||||
return { [tagName]: '' };
|
return { [tagName]: Object.keys(attributes).length ? attributes : '' };
|
||||||
}
|
}
|
||||||
|
|
||||||
pos++; // skip >
|
pos++; // skip >
|
||||||
const children: any[] = [];
|
const children: any[] = Object.entries(attributes).map(([k, v]) => ({ [k]: v }));
|
||||||
let textContent = '';
|
let textContent = '';
|
||||||
|
|
||||||
while(pos < xml.length) {
|
while(pos < xml.length) {
|
||||||
@@ -49,7 +49,6 @@ export function fromXml(xml: string) {
|
|||||||
pos++; // skip >
|
pos++; // skip >
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const startPos = pos;
|
|
||||||
const child = parseNode();
|
const child = parseNode();
|
||||||
if(typeof child === 'string') {
|
if(typeof child === 'string') {
|
||||||
textContent += child;
|
textContent += child;
|
||||||
@@ -64,17 +63,18 @@ export function fromXml(xml: string) {
|
|||||||
return { [tagName]: value };
|
return { [tagName]: value };
|
||||||
}
|
}
|
||||||
|
|
||||||
// If only text with no children
|
// If nothing at all
|
||||||
if(children.length === 0) {
|
if(children.length === 0) {
|
||||||
return { [tagName]: '' };
|
return { [tagName]: '' };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge children into object
|
// Merge attributes/children into object
|
||||||
const result: any = {};
|
const result: any = {};
|
||||||
|
if(textContent) result._text = isNumeric(textContent) ? Number(textContent) : textContent;
|
||||||
for(const child of children) {
|
for(const child of children) {
|
||||||
for(const [key, value] of Object.entries(child)) {
|
for(const [key, value] of Object.entries(child)) {
|
||||||
if(result[key]) {
|
if(result[key]) {
|
||||||
// Convert to array if duplicate tags
|
// Convert to array if duplicate tags/attrs
|
||||||
if(!Array.isArray(result[key])) {
|
if(!Array.isArray(result[key])) {
|
||||||
result[key] = [result[key]];
|
result[key] = [result[key]];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ describe('XML Parser', () => {
|
|||||||
expect(result).toEqual({ item: '' });
|
expect(result).toEqual({ item: '' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should parse tag with attributes (ignored in fast-xml-parser format)', () => {
|
it('should parse tag with attributes as merged properties', () => {
|
||||||
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({ user: '' });
|
expect(result).toEqual({ user: { id: '1', name: 'someone' } });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should parse tag with text content', () => {
|
it('should parse tag with text content', () => {
|
||||||
@@ -94,6 +94,8 @@ describe('XML Parser', () => {
|
|||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
root: {
|
root: {
|
||||||
user: {
|
user: {
|
||||||
|
id: '1',
|
||||||
|
name: 'someone',
|
||||||
email: 'someone@example.com',
|
email: 'someone@example.com',
|
||||||
active: ''
|
active: ''
|
||||||
}
|
}
|
||||||
@@ -190,7 +192,7 @@ describe('XML Parser', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('round-trip', () => {
|
describe('round-trip', () => {
|
||||||
it('should parse toXml output back to fast-xml-parser format', () => {
|
it('should parse toXml output back with attributes merged as properties', () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
tag: 'root',
|
tag: 'root',
|
||||||
attributes: { id: '1' },
|
attributes: { id: '1' },
|
||||||
@@ -202,6 +204,7 @@ describe('XML Parser', () => {
|
|||||||
const parsed = fromXml(xml);
|
const parsed = fromXml(xml);
|
||||||
expect(parsed).toEqual({
|
expect(parsed).toEqual({
|
||||||
root: {
|
root: {
|
||||||
|
id: '1',
|
||||||
child: 'text'
|
child: 'text'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user