Skip to content

Commit e34cc92

Browse files
committed
Color modification functions should take in 't', not 'option(t)'
1 parent a8994ae commit e34cc92

File tree

2 files changed

+86
-85
lines changed

2 files changed

+86
-85
lines changed

__tests__/Tinycolor_tests.re

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -274,88 +274,94 @@ describe("string representation methods", () => {
274274
describe("color modification tests", () => {
275275
let red = TinyColor.makeFromString("red");
276276

277-
test("lighten(int, t)", () => {
278-
let a = TinyColor.lighten(~value=10, red);
279-
expect(Option.map(a, TinyColor.toHexString))
280-
|> toEqual(Some("#ff3333"));
281-
});
277+
let optionMapTwo = (color, fn1, fn2) =>
278+
Option.flatMap(color, fn1)->Option.map(fn2);
282279

283-
test("lighten(int, t) does not change original instance", () => {
284-
let a = TinyColor.lighten(~value=10, red);
280+
test("lighten(int, t)", () =>
281+
optionMapTwo(red, TinyColor.lighten(~value=10), TinyColor.toHexString)
282+
|> expect
283+
|> toEqual(Some("#ff3333"))
284+
);
285285

286-
expect(Option.map(a, TinyColor.toHex8String))
286+
test("lighten(int, t) does not change original instance", () =>
287+
optionMapTwo(red, TinyColor.lighten(~value=10), TinyColor.toHex8String)
288+
|> expect
287289
|> not
288-
|> toEqual(Option.map(red, TinyColor.toHex8String));
289-
});
290-
291-
test("brighten(int, t)", () => {
292-
let a = TinyColor.brighten(~value=10, red);
293-
expect(Option.map(a, TinyColor.toHexString))
294-
|> toEqual(Some("#ff1919"));
295-
});
290+
|> toEqual(Option.map(red, TinyColor.toHex8String))
291+
);
296292

297-
test("brighten(int, t) dose not change original instance", () => {
298-
let a = TinyColor.brighten(~value=10, red);
293+
test("brighten(int, t)", () =>
294+
optionMapTwo(red, TinyColor.brighten(~value=10), TinyColor.toHexString)
295+
|> expect
296+
|> toEqual(Some("#ff1919"))
297+
);
299298

300-
expect(Option.map(a, TinyColor.toString))
299+
test("brighten(int, t) dose not change original instance", () =>
300+
optionMapTwo(red, TinyColor.brighten(~value=10), TinyColor.toString)
301+
|> expect
301302
|> not
302-
|> toEqual(Option.map(red, TinyColor.toString));
303-
});
303+
|> toEqual(Option.map(red, TinyColor.toString))
304+
);
304305

305-
test("darken(int, t)", () => {
306-
let a = TinyColor.darken(~value=10, red);
307-
expect(Option.map(a, TinyColor.toHexString))
308-
|> toEqual(Some("#cc0000"));
309-
});
306+
test("darken(int, t)", () =>
307+
optionMapTwo(red, TinyColor.darken(~value=10), TinyColor.toHexString)
308+
|> expect
309+
|> toEqual(Some("#cc0000"))
310+
);
310311

311-
test("tint(int, t)", () => {
312-
let a = TinyColor.tint(~value=10, red);
313-
expect(Option.map(a, TinyColor.toHexString))
314-
|> toEqual(Some("#ff1a1a"));
315-
});
312+
test("tint(int, t)", () =>
313+
optionMapTwo(red, TinyColor.tint(~value=10), TinyColor.toHexString)
314+
|> expect
315+
|> toEqual(Some("#ff1a1a"))
316+
);
316317

317-
test("shade(int, t)", () => {
318-
let a = TinyColor.shade(~value=10, red);
319-
expect(Option.map(a, TinyColor.toHexString))
320-
|> toEqual(Some("#e60000"));
321-
});
318+
test("shade(int, t)", () =>
319+
optionMapTwo(red, TinyColor.shade(~value=10), TinyColor.toHexString)
320+
|> expect
321+
|> toEqual(Some("#e60000"))
322+
);
322323

323-
test("desaturate(int, t)", () => {
324-
let a = TinyColor.desaturate(~value=10, red);
325-
expect(Option.map(a, TinyColor.toHexString))
326-
|> toEqual(Some("#f20d0d"));
327-
});
324+
test("desaturate(int, t)", () =>
325+
optionMapTwo(red, TinyColor.desaturate(~value=10), TinyColor.toHexString)
326+
|> expect
327+
|> toEqual(Some("#f20d0d"))
328+
);
328329

329-
test("saturate(int, t)", () => {
330-
let a = TinyColor.saturate(~value=10, red);
331-
expect(Option.map(a, TinyColor.toHexString))
332-
|> toEqual(Some("#ff0000"));
333-
});
330+
test("saturate(int, t)", () =>
331+
optionMapTwo(red, TinyColor.saturate(~value=10), TinyColor.toHexString)
332+
|> expect
333+
|> toEqual(Some("#ff0000"))
334+
);
334335

335-
test("spin(int, t)", () => {
336-
let a = TinyColor.spin(~value=180, red);
337-
expect(Option.map(a, TinyColor.toHex8String)) === Some("#00ffffff");
338-
});
336+
test("spin(int, t)", () =>
337+
Option.map(red, TinyColor.spin(~value=180))
338+
->Option.map(TinyColor.toHex8String)
339+
|> expect === Some("#00ffffff")
340+
);
339341

340-
test("spin(int, t) with value 360 does not change", () => {
341-
let a = TinyColor.spin(~value=360, red);
342-
expect(Option.map(a, TinyColor.toHex8String))
343-
=== Option.map(red, TinyColor.toHex8String);
344-
});
342+
test("spin(int, t) with value 360 does not change", () =>
343+
Option.map(red, TinyColor.spin(~value=360))
344+
->Option.map(TinyColor.toHex8String)
345+
|> expect === Option.map(red, TinyColor.toHex8String)
346+
);
345347

346348
test("mix(int, t)", () => {
347349
let color1 = TinyColor.makeFromString("#f0f");
348350
let color2 = TinyColor.makeFromString("#0f0");
349351

350-
let a = TinyColor.mix(~value=50, color1, color2);
351-
expect(Option.map(a, TinyColor.toHexString))
352-
|> toEqual(Some("#808080"));
352+
switch (color1, color2) {
353+
| (Some(c1), Some(c2)) =>
354+
TinyColor.mix(~value=50, c1, c2)->Option.map(TinyColor.toHexString)
355+
|> expect
356+
|> toEqual(Some("#808080"))
357+
| _ => expect(false) === true
358+
};
353359
});
354360

355-
test("greyscale(t)", () => {
356-
let a = TinyColor.greyscale(red);
357-
expect(Option.map(a, TinyColor.toHex8String)) === Some("#808080ff");
358-
});
361+
test("greyscale(t)", () =>
362+
Option.map(red, TinyColor.greyscale)->Option.map(TinyColor.toHex8String)
363+
|> expect === Some("#808080ff")
364+
);
359365
});
360366

361367
describe("color combinations", () => {

src/TinyColor.re

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -269,49 +269,44 @@ let callIfValidModificationValue = (value, modFun, color) =>
269269
};
270270

271271
[@bs.send] external lighten: (t, int) => t = "";
272-
let lighten = (~value: int=10, color: option(t)) =>
273-
Option.flatMap(color, callIfValidModificationValue(value, lighten));
272+
let lighten = (~value: int=10, color: t) =>
273+
callIfValidModificationValue(value, lighten, color);
274274

275275
[@bs.send] external brighten: (t, int) => t = "";
276-
let brighten = (~value: int=10, color: option(t)) =>
277-
Option.flatMap(color, callIfValidModificationValue(value, brighten));
276+
let brighten = (~value: int=10, color: t) =>
277+
callIfValidModificationValue(value, brighten, color);
278278

279279
[@bs.send] external darken: (t, int) => t = "";
280-
let darken = (~value: int=10, color: option(t)) =>
281-
Option.flatMap(color, callIfValidModificationValue(value, darken));
280+
let darken = (~value: int=10, color: t) =>
281+
callIfValidModificationValue(value, darken, color);
282282

283283
[@bs.send] external tint: (t, int) => t = "";
284-
let tint = (~value: int=10, color: option(t)) =>
285-
Option.flatMap(color, callIfValidModificationValue(value, tint));
284+
let tint = (~value: int=10, color: t) =>
285+
callIfValidModificationValue(value, tint, color);
286286

287287
[@bs.send] external shade: (t, int) => t = "";
288-
let shade = (~value: int=10, color: option(t)) =>
289-
Option.flatMap(color, callIfValidModificationValue(value, shade));
288+
let shade = (~value: int=10, color: t) =>
289+
callIfValidModificationValue(value, shade, color);
290290

291291
[@bs.send] external desaturate: (t, int) => t = "";
292-
let desaturate = (~value: int=10, color: option(t)) =>
293-
Option.flatMap(color, callIfValidModificationValue(value, desaturate));
292+
let desaturate = (~value: int=10, color: t) =>
293+
callIfValidModificationValue(value, desaturate, color);
294294

295295
[@bs.send] external saturate: (t, int) => t = "";
296-
let saturate = (~value: int=10, color: option(t)) =>
297-
Option.flatMap(color, callIfValidModificationValue(value, saturate));
296+
let saturate = (~value: int=10, color: t) =>
297+
callIfValidModificationValue(value, saturate, color);
298298

299299
[@bs.send] external spin: (t, int) => t = "";
300-
let spin = (~value: int=10, color: option(t)) => {
301-
let callSpin = color' => spin(color', value);
302-
Option.map(color, callSpin);
300+
let spin = (~value: int=10, color: t) => {
301+
spin(color, value);
303302
};
304303

305304
[@bs.send] external mix: (t, t, int) => t = "";
306-
let mix = (~value: int=50, color1: option(t), color2: option(t)) => {
307-
switch (color1, color2) {
308-
| (Some(c1), Some(c2)) => callIfValidModificationValue(value, mix(c1), c2)
309-
| _ => None
310-
};
305+
let mix = (~value: int=50, color1: t, color2: t) => {
306+
callIfValidModificationValue(value, mix(color1), color2);
311307
};
312308

313309
[@bs.send] external greyscale: t => t = "";
314-
let greyscale = (color: option(t)) => Option.map(color, greyscale);
315310

316311
/* COLOR COMBINATIONS */
317312

0 commit comments

Comments
 (0)