Skip to content

Commit ed823c0

Browse files
committedMay 28, 2024·
Minor refactor: space.getFormat()
1 parent 55468d2 commit ed823c0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed
 

‎src/space.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,27 @@ export default class ColorSpace {
136136
return false;
137137
}
138138

139+
/**
140+
* Lookup a format in this color space
141+
* @param {string | object | Format} format - Format id if string. If object, it's converted to a `Format` object and returned.
142+
* @returns {Format}
143+
*/
139144
getFormat (format) {
140145
if (!format) {
141146
return null;
142147
}
143148

144-
if (!(typeof format === "object")) {
145-
let name = format === "default" ? Object.keys(this.formats)[0] : format;
146-
format = this.formats[name];
149+
if (format === "default") {
150+
format = Object.values(this.formats)[0];
151+
}
152+
else if (typeof format === "string") {
153+
format = this.formats[format];
147154
}
148155

149156
let ret = Format.get(format, this);
150157

151158
if (ret !== format && format.name in this.formats) {
159+
// Update the format we have on file so we can find it more quickly next time
152160
this.formats[format.name] = ret;
153161
}
154162

@@ -307,9 +315,9 @@ export default class ColorSpace {
307315

308316
/**
309317
* Look up all color spaces for a format that matches certain criteria
310-
* @param {*} name
311-
* @param {*} filters
318+
* @param {object | string} filters
312319
* @param {Array<ColorSpace>} [spaces=ColorSpace.all]
320+
* @returns {Format | null}
313321
*/
314322
static findFormat (filters, spaces = ColorSpace.all) {
315323
if (!filters) {

0 commit comments

Comments
 (0)