UNPKG

902 BPlain TextView Raw
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 */
8
9import {groupBy} from 'lodash';
10import mime from 'mime';
11
12/**
13 * Given an array of files, it groups it by it's type.
14 * Type of the file is inferred from it's mimetype based on the extension
15 * file ends up with. The returned value is an object with properties that
16 * correspond to the first part of the mimetype, e.g. images will be grouped
17 * under `image` key since the mimetype for them is `image/jpg` etc.
18 *
19 * Example:
20 * Given an array ['fonts/a.ttf', 'images/b.jpg'],
21 * the returned object will be: {font: ['fonts/a.ttf'], image: ['images/b.jpg']}
22 */
23export default function groupFilesByType(assets: Array<string>) {
24 return groupBy(assets, (type) => (mime.getType(type) || '').split('/')[0]);
25}