Skip to content

Commit 65e58f2

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: @react-native/normalize-color support Node.js
Summary: Changes `react-native/normalize-color` to be useable from Node.js by making the following changes: 1. Rename `base.js` to `index.js` so importing is more convenient. 2. Move Flow definitions into a seprate library definition flow so `index.js` can be consumed directly. I also made a few improvements to the actual implementation: 1. Avoid allocating `matchers` for non-strings. 2. Avoid allocating an object of all the color keywords. This will reduce memory usage (in exchange for slightly larger compiled bytecode). Changelog: [General][Changed] - react-native/normalize-color now supports Node.js Reviewed By: lunaleaps Differential Revision: D30595908 fbshipit-source-id: e6279e9ff815d8d1f489811187deabfdf53b8fbf
1 parent b26f277 commit 65e58f2

File tree

9 files changed

+527
-526
lines changed

9 files changed

+527
-526
lines changed

Libraries/StyleSheet/__tests__/normalizeColor-test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
const {OS} = require('../../Utilities/Platform');
1414
const normalizeColor = require('../normalizeColor');
1515

16-
it('forwards calls to @react-native/normalize-color/base', () => {
17-
jest
18-
.resetModules()
19-
.mock('@react-native/normalize-color/base', () => jest.fn());
16+
it('forwards calls to @react-native/normalize-color', () => {
17+
jest.resetModules().mock('@react-native/normalize-color', () => jest.fn());
2018

2119
expect(require('../normalizeColor')('#abc')).not.toBe(null);
22-
expect(require('@react-native/normalize-color/base')).toBeCalled();
20+
expect(require('@react-native/normalize-color')).toBeCalled();
2321
});
2422

2523
describe('iOS', () => {

Libraries/StyleSheet/normalizeColor.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/* eslint no-bitwise: 0 */
1212

13-
import normalizeColorBase from '@react-native/normalize-color/base';
13+
import _normalizeColor from '@react-native/normalize-color';
1414

1515
import type {ColorValue} from './StyleSheet';
1616
import type {ProcessedColorValue} from './processColor';
@@ -27,7 +27,7 @@ function normalizeColor(
2727
}
2828

2929
if (typeof color === 'string' || typeof color === 'number') {
30-
return normalizeColorBase(color);
30+
return _normalizeColor(color);
3131
}
3232
}
3333

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"@react-native-community/cli-platform-android": "^6.0.0",
9595
"@react-native-community/cli-platform-ios": "^6.0.0",
9696
"@react-native/assets": "1.0.0",
97-
"@react-native/normalize-color": "1.0.0",
97+
"@react-native/normalize-color": "2.0.0",
9898
"@react-native/polyfills": "2.0.0",
9999
"abort-controller": "^3.0.0",
100100
"anser": "^1.4.9",

packages/normalize-color/__tests__/base-test.js

-129
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+react_native
8+
* @flow stroct
9+
* @format
10+
*/
11+
12+
'use strict';
13+
14+
import normalizeColor from '..';
15+
16+
it('accepts only spec compliant colors', () => {
17+
expect(normalizeColor('#abc')).not.toBe(null);
18+
expect(normalizeColor('#abcd')).not.toBe(null);
19+
expect(normalizeColor('#abcdef')).not.toBe(null);
20+
expect(normalizeColor('#abcdef01')).not.toBe(null);
21+
expect(normalizeColor('rgb(1,2,3)')).not.toBe(null);
22+
expect(normalizeColor('rgb(1, 2, 3)')).not.toBe(null);
23+
expect(normalizeColor('rgb( 1 , 2 , 3 )')).not.toBe(null);
24+
expect(normalizeColor('rgb(-1, -2, -3)')).not.toBe(null);
25+
expect(normalizeColor('rgba(0, 0, 0, 1)')).not.toBe(null);
26+
expect(normalizeColor(0x01234567 + 0.5)).toBe(null);
27+
expect(normalizeColor(-1)).toBe(null);
28+
expect(normalizeColor(0xffffffff + 1)).toBe(null);
29+
});
30+
31+
it('temporarilys accept floating point values for rgb', () => {
32+
expect(normalizeColor('rgb(1.1, 2.1, 3.1)')).toBe(0x010203ff);
33+
expect(normalizeColor('rgba(1.1, 2.1, 3.1, 1.0)')).toBe(0x010203ff);
34+
});
35+
36+
it('refuses non-spec compliant colors', () => {
37+
expect(normalizeColor('#00gg00')).toBe(null);
38+
expect(normalizeColor('rgb(1, 2, 3,)')).toBe(null);
39+
expect(normalizeColor('rgb(1, 2, 3')).toBe(null);
40+
41+
// Used to be accepted by normalizeColor
42+
expect(normalizeColor('abc')).toBe(null);
43+
expect(normalizeColor(' #abc ')).toBe(null);
44+
expect(normalizeColor('##abc')).toBe(null);
45+
expect(normalizeColor('rgb 255 0 0')).toBe(null);
46+
expect(normalizeColor('RGBA(0, 1, 2)')).toBe(null);
47+
expect(normalizeColor('rgb (0, 1, 2)')).toBe(null);
48+
expect(normalizeColor('hsv(0, 1, 2)')).toBe(null);
49+
// $FlowExpectedError - Intentionally malformed argument.
50+
expect(normalizeColor({r: 10, g: 10, b: 10})).toBe(null);
51+
expect(normalizeColor('hsl(1%, 2, 3)')).toBe(null);
52+
expect(normalizeColor('rgb(1%, 2%, 3%)')).toBe(null);
53+
});
54+
55+
it('handles hex6 properly', () => {
56+
expect(normalizeColor('#000000')).toBe(0x000000ff);
57+
expect(normalizeColor('#ffffff')).toBe(0xffffffff);
58+
expect(normalizeColor('#ff00ff')).toBe(0xff00ffff);
59+
expect(normalizeColor('#abcdef')).toBe(0xabcdefff);
60+
expect(normalizeColor('#012345')).toBe(0x012345ff);
61+
});
62+
63+
it('handles hex3 properly', () => {
64+
expect(normalizeColor('#000')).toBe(0x000000ff);
65+
expect(normalizeColor('#fff')).toBe(0xffffffff);
66+
expect(normalizeColor('#f0f')).toBe(0xff00ffff);
67+
});
68+
69+
it('handles hex8 properly', () => {
70+
expect(normalizeColor('#00000000')).toBe(0x00000000);
71+
expect(normalizeColor('#ffffffff')).toBe(0xffffffff);
72+
expect(normalizeColor('#ffff00ff')).toBe(0xffff00ff);
73+
expect(normalizeColor('#abcdef01')).toBe(0xabcdef01);
74+
expect(normalizeColor('#01234567')).toBe(0x01234567);
75+
});
76+
77+
it('handles rgb properly', () => {
78+
expect(normalizeColor('rgb(0, 0, 0)')).toBe(0x000000ff);
79+
expect(normalizeColor('rgb(-1, -2, -3)')).toBe(0x000000ff);
80+
expect(normalizeColor('rgb(0, 0, 255)')).toBe(0x0000ffff);
81+
expect(normalizeColor('rgb(100, 15, 69)')).toBe(0x640f45ff);
82+
expect(normalizeColor('rgb(255, 255, 255)')).toBe(0xffffffff);
83+
expect(normalizeColor('rgb(256, 256, 256)')).toBe(0xffffffff);
84+
});
85+
86+
it('handles rgba properly', () => {
87+
expect(normalizeColor('rgba(0, 0, 0, 0.0)')).toBe(0x00000000);
88+
expect(normalizeColor('rgba(0, 0, 0, 0)')).toBe(0x00000000);
89+
expect(normalizeColor('rgba(0, 0, 0, -0.5)')).toBe(0x00000000);
90+
expect(normalizeColor('rgba(0, 0, 0, 1.0)')).toBe(0x000000ff);
91+
expect(normalizeColor('rgba(0, 0, 0, 1)')).toBe(0x000000ff);
92+
expect(normalizeColor('rgba(0, 0, 0, 1.5)')).toBe(0x000000ff);
93+
expect(normalizeColor('rgba(100, 15, 69, 0.5)')).toBe(0x640f4580);
94+
});
95+
96+
it('handles hsl properly', () => {
97+
expect(normalizeColor('hsl(0, 0%, 0%)')).toBe(0x000000ff);
98+
expect(normalizeColor('hsl(360, 100%, 100%)')).toBe(0xffffffff);
99+
expect(normalizeColor('hsl(180, 50%, 50%)')).toBe(0x40bfbfff);
100+
expect(normalizeColor('hsl(540, 50%, 50%)')).toBe(0x40bfbfff);
101+
expect(normalizeColor('hsl(70, 25%, 75%)')).toBe(0xcacfafff);
102+
expect(normalizeColor('hsl(70, 100%, 75%)')).toBe(0xeaff80ff);
103+
expect(normalizeColor('hsl(70, 110%, 75%)')).toBe(0xeaff80ff);
104+
expect(normalizeColor('hsl(70, 0%, 75%)')).toBe(0xbfbfbfff);
105+
expect(normalizeColor('hsl(70, -10%, 75%)')).toBe(0xbfbfbfff);
106+
});
107+
108+
it('handles hsla properly', () => {
109+
expect(normalizeColor('hsla(0, 0%, 0%, 0)')).toBe(0x00000000);
110+
expect(normalizeColor('hsla(360, 100%, 100%, 1)')).toBe(0xffffffff);
111+
expect(normalizeColor('hsla(360, 100%, 100%, 0)')).toBe(0xffffff00);
112+
expect(normalizeColor('hsla(180, 50%, 50%, 0.2)')).toBe(0x40bfbf33);
113+
});
114+
115+
it('handles named colors properly', () => {
116+
expect(normalizeColor('red')).toBe(0xff0000ff);
117+
expect(normalizeColor('transparent')).toBe(0x00000000);
118+
expect(normalizeColor('peachpuff')).toBe(0xffdab9ff);
119+
});
120+
121+
it('handles number colors properly', () => {
122+
expect(normalizeColor(0x00000000)).toBe(0x00000000);
123+
expect(normalizeColor(0xff0000ff)).toBe(0xff0000ff);
124+
expect(normalizeColor(0xffffffff)).toBe(0xffffffff);
125+
expect(normalizeColor(0x01234567)).toBe(0x01234567);
126+
});
127+
128+
it('returns the same color when it is already normalized', () => {
129+
const normalizedColor = normalizeColor('red') || 0;
130+
expect(normalizeColor(normalizedColor)).toBe(normalizedColor);
131+
});

0 commit comments

Comments
 (0)