All files / packages/icons/.claude/skills/icons-release/scripts/lib svg.js

0% Statements 0/27
0% Branches 0/20
0% Functions 0/2
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                                                               
'use strict'
 
const fs = require('fs')
const { JSDOM } = require('jsdom')
 
function readSvg(filePath) {
  if (!filePath || !fs.existsSync(filePath)) return null
  return fs.readFileSync(filePath, 'utf8')
}
 
/**
 * Extract width/height from an SVG string. Falls back to the last two
 * viewBox values when width/height attributes are absent.
 * Returns { width, height } as numbers, or null if it can't be parsed.
 */
function getSvgDimensions(svgContent) {
  if (!svgContent) return null
  const svg = new JSDOM(svgContent).window.document.querySelector('svg')
  if (!svg) return null
 
  let w = svg.getAttribute('width')
  let h = svg.getAttribute('height')
 
  if (!w || !h) {
    const viewBox = svg.getAttribute('viewBox')
    if (!viewBox) return null
    const parts = viewBox.trim().split(/\s+/)
    if (parts.length < 4) return null
    w = parts[parts.length - 2]
    h = parts[parts.length - 1]
  }
 
  const width = Number(w)
  const height = Number(h)
  if (Number.isNaN(width) || Number.isNaN(height)) return null
  return { width, height }
}
 
module.exports = { readSvg, getSvgDimensions }