-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cleanupStyleAttributes): minify numeric values (#82)
- Loading branch information
1 parent
9a38290
commit 44d2109
Showing
12 changed files
with
288 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { LengthValue } from './length.js'; | ||
import { PctValue } from './numericvalue.js'; | ||
|
||
export class LengthOrPctValue { | ||
/** | ||
* @param {import('./types.js').SVGAttValue} value | ||
* @returns {LengthValue|PctValue} | ||
*/ | ||
static getLengthOrPctObj(value) { | ||
if (typeof value === 'string') { | ||
const v = value.trim(); | ||
if (v.endsWith('%')) { | ||
const pct = PctValue.createPctValue(v); | ||
if (pct) { | ||
return pct; | ||
} | ||
} | ||
} | ||
|
||
return LengthValue.getLengthObj(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { AttValue } from './attvalue.js'; | ||
import { ExactNum } from './exactnum.js'; | ||
import { isNumber, toFixed } from './svgo/tools.js'; | ||
|
||
export class NumericValue extends AttValue { | ||
#n; | ||
|
||
/** | ||
* @param {ExactNum} n | ||
*/ | ||
constructor(n) { | ||
super(undefined); | ||
this.#n = n; | ||
} | ||
|
||
generateString() { | ||
return this.#n.getMinifiedString(); | ||
} | ||
|
||
getMinifiedValue() { | ||
const value = this.#n.getValue(); | ||
// Use % if it can be represented as a single digit. | ||
if (value >= 0.01 && value <= 0.09 && this.#n.getNumberOfDigits() === 2) { | ||
return new PctValue(new ExactNum(value * 100)); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* @param {number} digits | ||
* @returns {AttValue} | ||
*/ | ||
round(digits) { | ||
const value = toFixed(this.#n.getValue(), digits); | ||
return new NumericValue(new ExactNum(value)).getMinifiedValue(); | ||
} | ||
} | ||
|
||
export class PctValue extends AttValue { | ||
#n; | ||
|
||
/** | ||
* @param {ExactNum} n | ||
*/ | ||
constructor(n) { | ||
super(undefined); | ||
this.#n = n; | ||
} | ||
|
||
/** | ||
* @param {string} value | ||
* @returns {PctValue|undefined} | ||
*/ | ||
static createPctValue(value) { | ||
const pct = value.substring(0, value.length - 1); | ||
if (isNumber(pct)) { | ||
return new PctValue(new ExactNum(pct)); | ||
} | ||
} | ||
|
||
generateString() { | ||
return this.#n.getMinifiedString() + '%'; | ||
} | ||
|
||
getMinifiedValue() { | ||
const pct = this.#n.getValue(); | ||
// Use % if it can be represented as a single digit. | ||
if (pct >= 1 && pct <= 9 && Number.isInteger(pct)) { | ||
return this; | ||
} | ||
return new NumericValue(new ExactNum(pct / 100)); | ||
} | ||
|
||
/** | ||
* @param {number} digits | ||
* @returns {AttValue} | ||
*/ | ||
round(digits) { | ||
return new NumericValue(new ExactNum(this.#n.getValue() / 100)).round( | ||
digits, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { AttValue } from './attvalue.js'; | ||
import { minifyNumber } from './svgo/tools.js'; | ||
|
||
export class OpacityValue extends AttValue { | ||
/** @type {string|undefined} */ | ||
#strVal; | ||
/** @type {number|undefined} */ | ||
#opacity; | ||
|
||
/** | ||
* @param {string|undefined} strVal | ||
* @param {number} [opacity] | ||
*/ | ||
constructor(strVal, opacity) { | ||
super(strVal); | ||
this.#opacity = opacity; | ||
} | ||
|
||
/** | ||
* @returns {number} | ||
*/ | ||
getOpacity() { | ||
if (this.#opacity === undefined) { | ||
// If opacity is not set, set it from the original string. | ||
this.#opacity = parseFloat(super.toString()); | ||
} | ||
return this.#opacity; | ||
} | ||
|
||
/** | ||
* @param {import('./types.js').SVGAttValue} value | ||
* @returns {OpacityValue} | ||
*/ | ||
static getOpacityObj(value) { | ||
if (typeof value === 'string') { | ||
return new OpacityValue(value); | ||
} | ||
if (value instanceof OpacityValue) { | ||
return value; | ||
} | ||
throw value; | ||
} | ||
|
||
/** | ||
* Override parent method to insure string is minified. | ||
* @returns {string} | ||
*/ | ||
toString() { | ||
if (this.#strVal === undefined) { | ||
// Minified string hasn't been generated yet. | ||
this.#strVal = minifyNumber(this.getOpacity()); | ||
} | ||
return this.#strVal; | ||
} | ||
} |
Oops, something went wrong.