tools

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package tools provides numeric and string utility functions ported from SVGO's lib/svgo/tools.js.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanupOutData

func CleanupOutData(data []float64, params *CleanupOutDataParams, command byte) string

CleanupOutData converts a row of numbers to an optimized string view.

Example: [0, -1, .5, .5] → "0-1 .5.5"

func DecodeSVGDataURI

func DecodeSVGDataURI(str string) string

DecodeSVGDataURI decodes a data URI back to an SVG string.

func EncodeSVGDataURI

func EncodeSVGDataURI(str string, dataType string) string

EncodeSVGDataURI encodes an SVG string as a data URI. Type can be "base64", "enc" (URI encoded), or "unenc" (unencoded).

func FindReferences

func FindReferences(attribute, value string) []string

FindReferences extracts all URL/href references from an attribute value.

func FormatNumber added in v0.2.0

func FormatNumber(f float64) string

FormatNumber formats a float64 the way JavaScript's Number.prototype.toString() does, which is what SVGO relies on for all numeric output.

func HasScripts

func HasScripts(node *svgast.Element) bool

HasScripts checks if a node contains any scripts. This checks the node's own properties, not parents or children.

func IncludesURLReference

func IncludesURLReference(body string) bool

IncludesURLReference checks if a string contains a url(#ref) reference.

func JSFalsy added in v0.2.0

func JSFalsy(v float64) bool

JSFalsy reports whether v is falsy as a JavaScript number: 0, -0 and NaN are, everything else is not. Pairs with JSIndex, since a missing element reads as NaN and JavaScript treats undefined as falsy too.

func JSIndex added in v0.2.0

func JSIndex(data []float64, i int) float64

JSIndex reads data[i] with JavaScript array semantics: an index past the end yields undefined, which behaves as NaN in every arithmetic context SVGO uses. Ports of SVGO code that destructures or indexes number arrays of unvalidated length (transform data, in particular) must go through this so short input produces NaN output instead of a panic.

func JSOr added in v0.2.0

func JSOr(v, alt float64) float64

JSOr mirrors the JavaScript `v || alt` idiom for numbers, substituting alt whenever v is falsy.

func JSRound added in v0.2.0

func JSRound(x float64) float64

JSRound rounds like JavaScript's Math.round: halves round toward +Infinity. Go's math.Round rounds halves away from zero, which disagrees with JS for negative X.5 values (e.g. -9124.5 → JS -9124 vs math.Round -9125) and can cascade into structurally different path output.

func NativeToFixed added in v0.2.0

func NativeToFixed(num float64, precision int) float64

NativeToFixed rounds num to precision decimals exactly as JavaScript's Number.prototype.toFixed followed by a numeric conversion does, i.e. `Number(num.toFixed(precision))`. The decision is made on the exact binary value of num and halves round away from zero:

NativeToFixed(-2.5, 0)    == -3   (ToFixed gives -2)
NativeToFixed(-0.0025, 3) == -0.003
NativeToFixed(1.005, 2)   == 1    (1.005 is below 1.005 as a double)

Neither ToFixed (halves toward +Infinity) nor strconv.FormatFloat (halves to even) can stand in for it. SVGO mixes both helpers, sometimes within one function — smartRound in plugins/_transforms.js guards with tools.toFixed but produces its values with the native method, and cleanupNumericValues / cleanupListOfValues use the native method throughout. Each ported call site must use the helper matching the JavaScript it mirrors.

func RemoveLeadingZero

func RemoveLeadingZero(value float64) string

RemoveLeadingZero removes the leading zero from floating-point numbers.

Examples: 0.5 → ".5", -0.5 → "-.5"

func ToFixed

func ToFixed(num float64, precision int) float64

ToFixed rounds a number to the specified precision, mirroring SVGO's tools.toFixed (`Math.round(num * 10 ** precision) / 10 ** precision`). Halves therefore round toward +Infinity: ToFixed(-2.5, 0) == -2.

This is NOT the same function as JavaScript's Number.prototype.toFixed — use NativeToFixed for ports of `x.toFixed(p)` call sites.

Types

type CleanupOutDataParams

type CleanupOutDataParams struct {
	NoSpaceAfterFlags  bool
	LeadingZero        bool
	NegativeExtraSpace bool
}

CleanupOutDataParams controls numeric output formatting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL