02020 / vite-kit

0 stars 0 forks source link

kit #8

Open 02020 opened 3 years ago

02020 commented 3 years ago
// kit 内部使用
const _ = {
  // 值判断
  value: (acc, key, value, i) => {
    if (Array.isArray(acc)) {
      acc[i] = value;
      return acc;
    } else if (Array.isArray(value) && value.length === 2) {
      acc[value[0]] = value[1];
    } else if (value != undefined) {
      // 不改变原key
      acc[key] = value;
    }
    return acc;
  },
};

//
const kit = {
  /**
   * 执行函数
   */
  o: (f) => (g) => (x) => f(g(x)),

  of: (x, flag = true) => (flag ? [x] : x),
  // 判断是否是数组
  fMapArray: (f, list) => (Array.isArray(list) ? list.map(f) : f(list)),
  // 执行map
  fMap: (f, cb = (x) => x) => (list) => cb(kit.fMapArray(f, list), list),
  /**
   *
   * @param {*} keys
   */
  keysC: (keys) => (arr) =>
    keys.reduce((acc, item, i) => ((acc[item] = arr[i]), acc), {}),
  /**
   *
   * @param {*} keys
   */
  keysTo: (keys) => kit.fMap(kit.keysC(keys)),

  // 入口判断
  fR: (f, initial = {}) => (o) =>
    Array.isArray(o) ? kit.fRa(f, initial)(o) : kit.fRo(f, initial)(o),
  // 数组
  fRa: (f, initial = {}) => (o) => {
    const fn = (acc, item, i) => _.value(acc, item, f(item, i, acc, o));
    return o.reduce(fn, initial);
  },
  fRo: (f, initial = {}) => (o) => {
    const fn = (acc, key, i) => _.value(acc, key, f(key, o[key], i, acc, o));
    return Object.keys(o).reduce(fn, initial);
  },

  // 未定名
  fxC: (fnC) => (key, value) => [key, () => kit.fMap(fnC)(value)],
};

/**
 * 应用-分组
 * @param {*} key
 */
kit.toGroup = (key) =>
  kit.fRa((item, index, acc) => {
    const group = item[key] || '';

    const initial = Array.isArray(acc[group]) ? acc[group] : [];
    initial.push(item);

    return [group, initial];
  });
/**
 * [  ['label', 'name','value'],  ]
 * @param {*} info
 */
kit.formItemC = (info) => (item) => {
  let value = info[item[1]],
    visible = true;
  if (!!item[3]) {
    value = item[3](value, info, item[1]);
  } else if (item[3] === false) {
    visible = false;
  }
  const resp = {
    label: item[0],
    name: item[1],
    value,
    class: item[2],
    visible,
  };

  return resp;
};

export default kit;
02020 commented 3 years ago

temp

const ____kit = {
  reduce: (arr, initial = {}) => (f) => arr.reduce(f, initial),
  keys: (keys, initial = {}) => (f) => Object.keys(keys).reduce(f, initial),
  mergeArray: (source, target) =>
    source.map((x, index) => {
      return Object.assign({}, x, target[index]);
    }),
};

const RR = {
  reduceO: (array, f) => array.reduce(f, {}),
  // 先分组在转换
  groupBy: (array, f) => RR.keysMap(array.reduce(f, {})),
};
02020 commented 3 years ago

demo 键值配对

const keys = ['a', 'b', 'c', 'd'];
const keyArray = [
  ['1', '2', '3', '4'],
  ['11', '22', '33', '44'],
];

const faa = (item, index) => {
  const o = kit.fR((item, i) => [keys[i], item])(item);
  return [index, o];
};
console.log(kit.fR(faa, [])(keyArray));

kit.keysTo: 数据中的 数组转对象

// 消息通知
const image = 'https://img-cdn-qiniu.dcloud.net.cn/new-page/uni.png';

const list = [
  [image, '缴费提醒', '您本月应缴费信息提醒', '8:35', 2],
  [image, '欠费补缴通知', '你到期未缴费欠费补缴通知', '昨天', 2],
  [image, '催缴涵', '欠费未补缴发送催缴涵', '昨天', 2],
  [image, '在线签约提醒', '电子合同在线签约提醒', '2020-09-08', 2],
  [image, '合同到期涵', '履约合同到期发送到期涵', '2020-09-08', 2],
  [image, '居住人信息补充通知', '租赁资产居住人信息录入通知', '2020-09-08', 2],
];

const keys = ['src', 'title', 'sub', 'topRight', 'num'];

kit.keysTo(keys)(list);
02020 commented 3 years ago

toGroup

const toArray = (key, item, index, acc) => item;
temp = kit.toGroup('group')(temp);
kit.fR(toArray, [])(temp);
02020 commented 3 years ago

demo 对象赋值对象 Set

const data = { a: '11', b: '22',};

const initial = { a: 'a1', aa: 'aa1', bb: 'bb2',};

const execute = (acc, key, value) => acc[key] = value;

const f = (key, value, index, acc) => execute(acc, key, value);

kit.fR(f, initial)(data);

console.log(initial);