react-native/Libraries/StyleSheet/flattenStyle.js

55 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2015-01-30 01:10:49 +00:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
2015-01-30 01:10:49 +00:00
*
* @providesModule flattenStyle
2015-03-25 02:34:12 +00:00
* @flow
* @format
2015-01-30 01:10:49 +00:00
*/
'use strict';
var ReactNativePropRegistry;
2015-01-30 01:10:49 +00:00
import type {DangerouslyImpreciseStyleProp} from 'StyleSheet';
import type {DangerouslyImpreciseStyle} from 'StyleSheetTypes';
2015-03-25 02:34:12 +00:00
2015-01-30 01:10:49 +00:00
function getStyle(style) {
if (ReactNativePropRegistry === undefined) {
ReactNativePropRegistry = require('ReactNativePropRegistry');
}
2015-01-30 01:10:49 +00:00
if (typeof style === 'number') {
return ReactNativePropRegistry.getByID(style);
2015-01-30 01:10:49 +00:00
}
return style;
}
function flattenStyle(
style: ?DangerouslyImpreciseStyleProp,
): ?DangerouslyImpreciseStyle {
if (style == null) {
2015-01-30 01:10:49 +00:00
return undefined;
}
if (!Array.isArray(style)) {
/* $FlowFixMe(>=0.63.0 site=react_native_fb) This comment suppresses an
* error found when Flow v0.63 was deployed. To see the error delete this
* comment and run Flow. */
2015-01-30 01:10:49 +00:00
return getStyle(style);
}
var result = {};
for (var i = 0, styleLength = style.length; i < styleLength; ++i) {
2015-01-30 01:10:49 +00:00
var computedStyle = flattenStyle(style[i]);
if (computedStyle) {
for (var key in computedStyle) {
result[key] = computedStyle[key];
}
2015-01-30 01:10:49 +00:00
}
}
return result;
}
module.exports = flattenStyle;