이름 마스킹 함수

Nekoromancer·2021년 12월 30일
0
import fp from "https://cdn.skypack.dev/lodash@4.17.21/fp";

const nameMasking = name => {
  const getLengthAndIdx = name => {
    const length = Math.floor(name.length / 2);
    return [
      name,
      length,
      Math.floor(length / 2) + (name.length % 2 === 0 ? 0 : 1),
    ];
  };

  const masking = ([name, length ,idx]) => [
    name.substr(0, idx), 
    fp.repeat(length, '*'),
    name.substr(idx + length, name.length),
  ];

  return fp.flow(
    fp.split(' '),
    fp.map(fp.flow(
      getLengthAndIdx,
      masking,
      fp.compact,
      fp.join(''),
    )),
    fp.join(' '),
  )(name)
};

const names = [
  '김구',
  '안중근',
  'John Smith',
  '小鳥遊 小鳥'
];


fp.map(nameMasking)(names);
// output: [
//  "*구",
//  "안*근",
//  "J**n Sm**h",
//  "小*遊 *鳥",
// ]
profile
고양이 앓이 중인 프론트엔드 개발자

0개의 댓글