Skip to content

Commit

Permalink
Remove humanizerTTL, dehumanizerTTL duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Comp0te committed Dec 23, 2024
1 parent b40dec9 commit b3e95f6
Showing 1 changed file with 2 additions and 60 deletions.
62 changes: 2 additions & 60 deletions src/utils/deprecated-deploy-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Result, Ok, Err } from 'ts-results';
import { concat } from '@ethersproject/bytes';
import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
import humanizeDuration from 'humanize-duration';
import fetch from 'node-fetch';
import {
Approval,
Expand All @@ -19,66 +18,9 @@ import {
import { CLPublicKey } from './deprecated-clPublicKey';
import { byteHash, toBytesU32 } from '../types/ByteConverters';
import { AsymmetricKey, validateSignature } from './deprecated-keys';
import {humanizerTTL, dehumanizerTTL } from '../types/SerializationUtils'

const shortEnglishHumanizer = humanizeDuration.humanizer({
spacer: '',
serialComma: false,
conjunction: ' ',
delimiter: ' ',
language: 'shortEn',
languages: {
// https://docs.rs/humantime/2.0.1/humantime/fn.parse_duration.html
shortEn: {
d: () => 'day',
h: () => 'h',
m: () => 'm',
s: () => 's',
ms: () => 'ms'
}
}
});

/**
* @deprecated
* Returns a humanizer duration
* @param ttl in milliseconds
* @returns A human-readable time in days, hours, minutes, seconds, then milliseconds
*/
export const humanizerTTL = (ttl: number) => {
return shortEnglishHumanizer(ttl);
};

/**
* @deprecated
* Returns duration in milliseconds
* @param ttl Human-readable string generated by [humanizerTTL](#L91)
* @returns The time-to-live in milliseconds
*/
export const dehumanizerTTL = (ttl: string): number => {
const dehumanizeUnit = (s: string): number => {
if (s.includes('ms')) {
return Number(s.replace('ms', ''));
}
if (s.includes('s') && !s.includes('m')) {
return Number(s.replace('s', '')) * 1000;
}
if (s.includes('m') && !s.includes('s')) {
return Number(s.replace('m', '')) * 60 * 1000;
}
if (s.includes('h')) {
return Number(s.replace('h', '')) * 60 * 60 * 1000;
}
if (s.includes('day')) {
return Number(s.replace('day', '')) * 24 * 60 * 60 * 1000;
}
throw Error('Unsuported TTL unit');
};

return ttl
.split(' ')
.map(dehumanizeUnit)
.reduce((acc, val) => (acc += val));
};
export {humanizerTTL, dehumanizerTTL};

/**
* @deprecated
Expand Down

0 comments on commit b3e95f6

Please sign in to comment.