react-native/Libraries/StyleSheet/flattenStyle.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-01-30 01:10:49 +00:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
2015-01-30 01:10:49 +00:00
*
* @providesModule flattenStyle
2015-03-25 02:34:12 +00:00
* @flow
2015-01-30 01:10:49 +00:00
*/
'use strict';
var invariant = require('fbjs/lib/invariant');
var ReactNativePropRegistry;
2015-01-30 01:10:49 +00:00
import type { StyleObj } 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: ?StyleObj): ?Object {
2015-01-30 01:10:49 +00:00
if (!style) {
return undefined;
}
2015-03-25 02:34:12 +00:00
invariant(style !== true, 'style may be false but not true');
2015-01-30 01:10:49 +00:00
if (!Array.isArray(style)) {
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;