Skip to content

Commit

Permalink
Merge pull request #156 from JolandaVerhoef/android-dp
Browse files Browse the repository at this point in the history
Output dp values in Android XML
  • Loading branch information
aputinski authored Jun 12, 2018
2 parents f29e549 + 6daade4 commit 7115b09
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/__tests__/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,13 @@ describe("relative/pixelValue", () => {
expect(t(p)).toEqual("5");
});
});

describe("absolute/dp", () => {
const t = get("absolute/dp");
it("converts px to dp", () => {
const p = Immutable.fromJS({
value: "1px"
});
expect(t(p)).toEqual("1dp");
});
});
1 change: 1 addition & 0 deletions lib/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ registerTransform("ios", [
registerTransform("android", [
"color/hex8argb",
"relative/pixelValue",
"absolute/dp",
"percentage/float"
]);

Expand Down
11 changes: 10 additions & 1 deletion lib/transforms/unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license

const { isRelativeSpacing, remToPx } = require("../util");
const {
isRelativeSpacing,
isAbsoluteSpacing,
remToPx,
pxToDp
} = require("../util");

const convertRemToPx = prop => {
const baseFontPercentage =
Expand All @@ -23,5 +28,9 @@ module.exports = {
"relative/pixelValue": {
predicate: prop => isRelativeSpacing(prop.get("value")),
transform: prop => convertRemToPx(prop).replace(/px$/g, "")
},
"absolute/dp": {
predicate: prop => isAbsoluteSpacing(prop.get("value")),
transform: prop => pxToDp(prop.get("value"))
}
};
5 changes: 5 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,24 @@ const allMatches = (s, pattern) => {
};

const isRelativeSpacing = value => /rem$/.test(value);
const isAbsoluteSpacing = value => /px$/.test(value);

const remToPx = (rem, baseFontPercentage, baseFontPixel) =>
parseFloat(rem.replace(/rem/g, "")) *
baseFontPixel *
(baseFontPercentage / 100) +
"px";

const pxToDp = px => px.replace(/px/g, "") + "dp";

const kebabCase = string => noCase(string, null, "-");

module.exports = {
allMatches,
isRelativeSpacing,
isAbsoluteSpacing,
remToPx,
pxToDp,
kebabCase,
indent,
comment
Expand Down

0 comments on commit 7115b09

Please sign in to comment.