1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import Pinyin from './Pinyin'; 17 18/** 19 * An util that provides sort for pinyin and other character(such as $#%^). 20 */ 21export class PinyinSort { 22 private readonly pinyin; 23 24 /** 25 * Constructor. 26 * 27 * @param {boolean} checkPolyphone - Whether to check for polyphonic words. 28 * @param {number} charCase - Output pinyin case mode, 0- first letter capitalization; 1- All lowercase; 2 - all uppercase. 29 */ 30 constructor(checkPolyphone = false, charCase = 1) { 31 this.pinyin = new Pinyin({ 32 checkPolyphone: checkPolyphone, 33 charCase: charCase 34 }); 35 } 36 37 /** 38 * Sort data for appinfo,compared by appName. 39 * 40 * @param {object} a - appinfo for compare. 41 * @return {object} b - appinfo for compare. 42 */ 43 sortByAppName(a, b) { 44 return this.getChar(a.appName) - this.getChar(b.appName); 45 } 46 47 /** 48 * Sort data for folderInfo,compared by folderName. 49 * 50 * @param {object} firstFolder - folderInfo for compare. 51 * @return {object} secondFolder - folderInfo for compare. 52 */ 53 sortByFolderName(firstFolder, secondFolder) { 54 return firstFolder.folderName.localeCompare(secondFolder.folderName); 55 } 56 57 /** 58 * Get first char for pinyin. 59 * 60 * @param {string} str - chinese string. 61 * @return {char} charCode. 62 */ 63 private getChar(str) { 64 return this.pinyin.getFullChars(str).charCodeAt(0); 65 } 66} 67