Skip to content

Commit

Permalink
fix: Revert #56 (#58)
Browse files Browse the repository at this point in the history
Closes #57
  • Loading branch information
diegohaz authored Jan 22, 2019
1 parent e49ce58 commit 14ec733
Show file tree
Hide file tree
Showing 15 changed files with 12 additions and 282 deletions.
12 changes: 3 additions & 9 deletions src/ifProp.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// @flow
/* eslint-disable no-use-before-define */
import prop from "./prop";
import resolveValue from "./resolveValue";
import type { Needle, PropsFn } from ".";

const parseFunction = (props: Object, test: Function): boolean => {
const result = test(props);
if (typeof result === "function") {
return parseFunction(props, result);
}
return Boolean(result);
};
const parseFunction = (props: Object, test: Function): boolean =>
Boolean(test(props));

const parseObject = (props: Object, test: Object): boolean => {
const keys = Object.keys(test);
Expand Down Expand Up @@ -68,7 +62,7 @@ const ifProp = (
}

const value = result ? pass : fail;
return resolveValue(value, props);
return typeof value === "function" ? value(props) : value;
};

export default ifProp;
10 changes: 3 additions & 7 deletions src/palette.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @flow
import resolveValue from "./resolveValue";
import type { PropsWithTheme } from ".";

type Props = PropsWithTheme & {
Expand Down Expand Up @@ -61,16 +60,13 @@ const palette = (
return finalDefaultValue;
}

const tones = toArray(resolveValue(props.theme.palette[key], props));
const tones = toArray(props.theme.palette[key]);

if (tone < 0) {
return resolveValue(
tones[clamp(tones.length + tone, 0, tones.length - 1)],
props
);
return tones[clamp(tones.length + tone, 0, tones.length - 1)];
}

return resolveValue(tones[clamp(tone, 0, tones.length - 1)], props);
return tones[clamp(tone, 0, tones.length - 1)];
};

export default palette;
5 changes: 2 additions & 3 deletions src/prop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @flow
import resolveValue from "./resolveValue";
import type { PropsFn } from ".";

/**
Expand All @@ -14,7 +13,7 @@ import type { PropsFn } from ".";
*/
const prop = (path: string, defaultValue?: any): PropsFn => (props = {}) => {
if (typeof props[path] !== "undefined") {
return resolveValue(props[path], props);
return props[path];
}

if (path && path.indexOf(".") > 0) {
Expand All @@ -29,7 +28,7 @@ const prop = (path: string, defaultValue?: any): PropsFn => (props = {}) => {
}

if (typeof object !== "undefined") {
return resolveValue(object, props);
return object;
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/resolveValue.js

This file was deleted.

7 changes: 2 additions & 5 deletions src/switchProp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow
import prop from "./prop";
import resolveValue from "./resolveValue";
import type { Needle, PropsFn } from ".";

/**
Expand Down Expand Up @@ -33,12 +32,10 @@ const switchProp = (
defaultCase: any
): PropsFn => (props = {}) => {
const value =
typeof needle === "function"
? resolveValue(needle, props)
: prop(needle)(props);
typeof needle === "function" ? needle(props) : prop(needle)(props);
const finalCases = typeof cases === "function" ? cases(props) : cases;
if (value in finalCases) {
return resolveValue(finalCases[value], props);
return finalCases[value];
}
return defaultCase;
};
Expand Down
2 changes: 1 addition & 1 deletion src/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import type { PropsWithTheme } from ".";
* `;
*/
const theme = (path: string, defaultValue?: any) => (props: PropsWithTheme) =>
prop(`theme.${path}`, defaultValue)(props);
prop(path, defaultValue)(props.theme);

export default theme;
7 changes: 1 addition & 6 deletions src/withProp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow
import prop from "./prop";
import resolveValue from "./resolveValue";
import type { Needle, PropsFn } from ".";

/**
Expand All @@ -25,11 +24,7 @@ const withProp = (needle: Needle | Needle[], fn: Function): PropsFn => (
return fn(...needles);
}
if (typeof needle === "function") {
const value = resolveValue(needle, props);
if (Array.isArray(value)) {
return withProp(value, fn)(props);
}
return fn(value);
return fn(needle(props));
}
return fn(prop(needle)(props));
};
Expand Down
53 changes: 0 additions & 53 deletions test/complex.test.js

This file was deleted.

23 changes: 0 additions & 23 deletions test/ifNotProp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,3 @@ test("function values", () => {
ifNotProp("foo", props => props.bar, props => props.foo)({ foo: "foo" })
).toBe("foo");
});

test("deep function argument", () => {
expect(ifNotProp(() => props => props.foo, "no", "yes")()).toBe("no");
expect(ifNotProp(() => props => props.foo, "no", "yes")({ foo: false })).toBe(
"no"
);
expect(ifNotProp(() => props => props.foo, "no", "yes")({ foo: true })).toBe(
"yes"
);
});

test("deep function values", () => {
expect(
ifNotProp("foo", () => props => props.bar, () => props => props.foo)({
bar: "bar"
})
).toBe("bar");
expect(
ifNotProp("foo", () => props => props.bar, () => props => props.foo)({
foo: "foo"
})
).toBe("foo");
});
35 changes: 0 additions & 35 deletions test/ifProp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,38 +92,3 @@ test("function values", () => {
ifProp("foo", props => props.foo, props => props.bar)({ foo: "foo" })
).toBe("foo");
});

test("deep function argument", () => {
expect(ifProp(() => props => props.foo, "yes", "no")()).toBe("no");
expect(ifProp(() => props => props.foo, "yes", "no")({ foo: false })).toBe(
"no"
);
expect(ifProp(() => props => props.foo, "yes", "no")({ foo: true })).toBe(
"yes"
);
});

test("deep function values", () => {
expect(
ifProp("foo", () => props => props.foo, () => props => props.bar)({
bar: "bar"
})
).toBe("bar");
expect(
ifProp("foo", () => props => props.foo, () => props => props.bar)({
foo: "foo"
})
).toBe("foo");
});

test("deep function array argument", () => {
expect(
ifProp([() => props => props.foo], "yes", "no")({ bar: false, foo: true })
).toBe("yes");
expect(
ifProp(["bar", () => props => props.foo], "yes", () => "no")({
bar: false,
foo: true
})
).toBe("no");
});
57 changes: 0 additions & 57 deletions test/palette.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ const theme = {
}
};

const deepTheme = {
palette: {
primary: [() => "primary0", () => "primary1", () => "primary2"],
secondary: () => "secondary0"
}
};

test("only tone", () => {
expect(palette()({ theme: {} })).toBeUndefined();
expect(palette()({ theme })).toBeUndefined();
Expand Down Expand Up @@ -51,53 +44,3 @@ test("palette and tone", () => {
expect(palette("other", 1)({ theme, palette: "primary" })).toBeUndefined();
expect(palette("other", 1, "foo")({ theme, palette: "primary" })).toBe("foo");
});

test("deep only tone", () => {
expect(palette()({ theme: deepTheme })).toBeUndefined();
expect(palette(0)({ theme: deepTheme })).toBeUndefined();
expect(palette()({ theme: deepTheme, palette: "primary" })).toBe("primary0");
expect(palette()({ theme: deepTheme, palette: "primary", tone: 2 })).toBe(
"primary2"
);
expect(palette(0)({ theme: deepTheme, palette: "primary" })).toBe("primary0");
expect(palette(1)({ theme: deepTheme, palette: "primary" })).toBe("primary1");
expect(palette(-1)({ theme: deepTheme, palette: "primary" })).toBe(
"primary2"
);
expect(palette(-5)({ theme: deepTheme, palette: "primary" })).toBe(
"primary0"
);
expect(palette(5)({ theme: deepTheme, palette: "primary" })).toBe("primary2");
expect(palette(0)({ theme: deepTheme, palette: "secondary" })).toBe(
"secondary0"
);
expect(palette(1)({ theme: deepTheme, palette: "secondary" })).toBe(
"secondary0"
);
expect(palette(0)({ theme: deepTheme, palette: "other" })).toBeUndefined();
expect(palette(1, "foo")({ theme: deepTheme })).toBe("foo");
expect(palette(1, "foo")({ theme: deepTheme, palette: "other" })).toBe("foo");
});

test("deep palette and tone", () => {
expect(palette("primary")({ theme: deepTheme })).toBe("primary0");
expect(palette("primary")({ theme: deepTheme, tone: 2 })).toBe("primary2");
expect(palette("primary", 0)({ theme: deepTheme })).toBe("primary0");
expect(palette("primary", 5)({ theme: deepTheme })).toBe("primary2");
expect(palette("primary", -1)({ theme: deepTheme })).toBe("primary2");
expect(palette("primary", -5)({ theme: deepTheme })).toBe("primary0");
expect(palette("secondary", 0)({ theme: deepTheme })).toBe("secondary0");
expect(palette("secondary", 1)({ theme: deepTheme })).toBe("secondary0");
expect(palette("secondary", 1, "foo")({ theme: deepTheme })).toBe(
"secondary0"
);
expect(palette("other", 1)({ theme: deepTheme })).toBeUndefined();
expect(palette("other", "foo")({ theme: deepTheme })).toBe("foo");
expect(palette("other", 1, "foo")({ theme: deepTheme })).toBe("foo");
expect(
palette("other", 1)({ theme: deepTheme, palette: "primary" })
).toBeUndefined();
expect(
palette("other", 1, "foo")({ theme: deepTheme, palette: "primary" })
).toBe("foo");
});
10 changes: 0 additions & 10 deletions test/prop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ test("string argument", () => {
expect(prop("color")()).toBeUndefined();
expect(prop("color")({})).toBeUndefined();
expect(prop("color")({ color: "red" })).toBe("red");
expect(prop("color")({ color: () => "red" })).toBe("red");
expect(prop("color")({ color: props => props.bg, bg: "red" })).toBe("red");
});

test("deep string argument", () => {
expect(prop("color.primary")()).toBeUndefined();
expect(prop("color.primary")({})).toBeUndefined();
expect(prop("color.primary")({ color: {} })).toBeUndefined();
expect(prop("color.primary")({ color: { primary: "red" } })).toBe("red");
expect(prop("color.primary")({ color: { primary: () => "red" } })).toBe(
"red"
);
expect(
prop("color.primary")({
color: { primary: prop("color.secondary"), secondary: "blue" }
})
).toBe("blue");
});

test("defaultValue", () => {
Expand Down
13 changes: 0 additions & 13 deletions test/switchProp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ test("string argument", () => {
expect(
switchProp("type", { red: "red", blue: "blue" })({ type: "blue" })
).toBe("blue");
expect(
switchProp("type", { red: "red", blue: "blue" })({ type: () => "red" })
).toBe("red");
expect(
switchProp("type", { red: () => "red", blue: () => "blue" })({
type: "red"
})
).toBe("red");
});

test("deep string argument", () => {
Expand Down Expand Up @@ -43,11 +35,6 @@ test("function argument", () => {
type: "blue"
})
).toBe("blue");
expect(
switchProp(() => props => props.type, { red: "red", blue: "blue" })({
type: "red"
})
).toBe("red");
});

test("default case", () => {
Expand Down
Loading

0 comments on commit 14ec733

Please sign in to comment.