2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

typescript uuid桁数を短くする

Last updated at Posted at 2022-05-01

uuid桁数はbase64エンコードで32桁→22桁に減らすことができます!
(typescriptでの書き方が分からず苦戦したのでメモしておきます)

id生成ライブラリはuuidv4を使っています

const originId = uuidv4();
console.log(`変換前のuuid: ${originId}`);

// uuidを16進数に変換した配列
const hexArray: number[] = originId.split('-').join('').split('', 16)
    .map((hex: string) => parseInt(hex, 16));
            
const shortId = Buffer.from(hexArray).toString('base64').replace('==', '');
console.log(`base64で短縮: ${shortId}`);

このように桁数を削減することができました!!

変換前のuuid: 5728e2ef-7c01-4d69-934d-197742bcfbff
base64で短縮: BQcCCA4CDg8HDAABBA0GCQ
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?