( styles: ViewStyle, ...filters: Tuple )
| 20 | * - A style property will exist in a single style object, the first filter it matched |
| 21 | */ |
| 22 | export function splitStyles<Tuple extends FiltersArray>( |
| 23 | styles: ViewStyle, |
| 24 | ...filters: Tuple |
| 25 | ) { |
| 26 | if (process.env.NODE_ENV !== 'production' && filters.length === 0) { |
| 27 | console.error('No filters were passed when calling splitStyles'); |
| 28 | } |
| 29 | |
| 30 | // `Object.entries` will be used to iterate over the styles and `Object.fromEntries` will be called before returning |
| 31 | // Entries which match the given filters will be temporarily stored in `newStyles` |
| 32 | const newStyles = filters.map(() => [] as Entry[]); |
| 33 | |
| 34 | // Entries which match no filter |
| 35 | const rest: Entry[] = []; |
| 36 | |
| 37 | // Iterate every style property |
| 38 | outer: for (const item of Object.entries(styles) as Entry[]) { |
| 39 | // Check each filter |
| 40 | for (let i = 0; i < filters.length; i++) { |
| 41 | // Check if filter matches |
| 42 | if (filters[i](item[0])) { |
| 43 | newStyles[i].push(item); // Push to temporary filtered entries array |
| 44 | continue outer; // Skip to checking next style property |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Adds to rest styles if not filtered |
| 49 | rest.push(item); |
| 50 | } |
| 51 | |
| 52 | // Put unmatched styles in the beginning |
| 53 | newStyles.unshift(rest); |
| 54 | |
| 55 | // Convert arrays of entries into objects |
| 56 | return newStyles.map((styles) => Object.fromEntries(styles)) as unknown as [ |
| 57 | ViewStyle, |
| 58 | ...MappedTuple<Tuple> |
| 59 | ]; |
| 60 | } |
no test coverage detected
searching dependent graphs…