blob: 4c2d5f8ba6463d4d56c659affdf85d8e3bfeebb1 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Utils from '../utils';
/**
* 根据26个字母取每一项首字母对数据进行排序,处理数据变换
* @return {[array]}
*/
export function totalList (source, hotListConfig, cityLocationConfig) {
const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const res = [];
LETTERS.split('').forEach(letter => {
const _data = source.filter(item => {
if (item.pinYin) {
return item.pinYin.slice(0, 1).toLowerCase() === letter.toLowerCase();
} else if (item.py) {
return item.py.slice(0, 1).toLowerCase() === letter.toLowerCase();
} else {
return false;
}
});
if (_data.length) {
res.push({
title: letter,
data: _data,
type: 'list'
});
}
});
// 处理热门数据
const hotList = getSpecialData(hotListConfig);
hotList && res.unshift(hotList);
// 处理特殊定位数据
const cityLocation = getSpecialData(cityLocationConfig);
cityLocation && res.unshift(cityLocation);
return res;
}
export function getSpecialData (data) {
if (data && data.type && data.list && data.list.length > 0) {
const { type, title, list } = data;
return {
title,
type,
data: type === 'group' ? Utils.arrayChunk(list) : list
};
} else {
return null;
}
}