vue3 工具函数封装 tool.ts

1. 存储封装
export const setStorage = (key: any, value: any) => {
  return Taro.setStorageSync(key, value);
};

export const getStorage = (key: any) => {
  return Taro.getStorageSync(key);
};

export const clearStorage = (key: any) => {
  return Taro.removeStorageSync(key);
};



2.将26 个英文字母 分类城市列表 
import pinyin from "pinyin";


export const transformDataToNewFormat = (data) => {
  const newData = [];

  // 创建包含 A 到 Z 的字母的数组
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");

  // 初始化新数据结构
  alphabet.forEach((letter) => {
    newData.push({
      title: letter,
      list: [],
    });
  });

  // 将城市数据分组到对应的字母标题下
  data.forEach((city) => {
    const firstLetter = city.cityName[0].toUpperCase();
    const pinyinArray = pinyin(firstLetter, {
      style: pinyin.STYLE_NORMAL, // 使用普通风格的拼音
    });

    const pinyinFirstLetter =
      pinyinArray.length > 0 ? pinyinArray[0][0][0].toUpperCase() : firstLetter;

    const index = alphabet.indexOf(pinyinFirstLetter);
    if (index !== -1) {
      newData[index].list.push({ id: city.id, name: city.cityName });
    }
  });

  return newData;
};

你可能感兴趣的:(javascript,typescript,开发语言)