Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save m4l3vich/840e5440b494929dd04ca123ed96078b to your computer and use it in GitHub Desktop.
Save m4l3vich/840e5440b494929dd04ca123ed96078b to your computer and use it in GitHub Desktop.
Number.toLocaleString() Enterprise Edition
export const NUMBER_ZERO_AS_BASE_2_STRING = '0'
export const NUMBER_THREE_AS_BASE_4_STRING = '3'
export const NUMBER_FORTY_SIX_AS_BASE_35_STRING = '1b'
export const NUMBER_ZERO_IN_BASE_10 = parseInt(NUMBER_ZERO_AS_BASE_2_STRING, 2)
export const NUMBER_THREE_IN_BASE_10 = parseInt(NUMBER_THREE_AS_BASE_4_STRING, 4)
export const NUMBER_FORTY_SIX_IN_BASE_10 = parseInt(NUMBER_FORTY_SIX_AS_BASE_35_STRING, 35)
export const PLACE_VALUE_LENGTH = NUMBER_THREE_IN_BASE_10
export const DOT_CHARACTER_CODE = NUMBER_FORTY_SIX_IN_BASE_10
export const DOT_CHARACTER_AS_STRING = String.fromCharCode(DOT_CHARACTER_CODE)
export const EMPTY_ARRAY = new Array(NUMBER_ZERO_IN_BASE_10)
export const JAVASCRIPT_NUMBER_TYPE_NAME = 'number'
export const NUMBER_VALIDATION_ERROR_NAME = 'NumberTypeValidationError'
export const NUMBER_VALIDATION_ERROR_MESSAGE = 'Input type should be "' + JAVASCRIPT_NUMBER_TYPE_NAME + '"'
export class NumberTypeValidationError extends Error {
constructor (errorMessageString) {
super(errorMessageString)
this.name = NUMBER_VALIDATION_ERROR_NAME
}
}
export function formatNumberBySplittingPlaceValuesWithDots (inputNumberArgument) {
if (typeof inputNumberArgument !== JAVASCRIPT_NUMBER_TYPE_NAME) {
throw new NumberTypeValidationError(NUMBER_VALIDATION_ERROR_MESSAGE)
}
const numberWithoutFractionalPart = Math.floor(inputNumberArgument)
const wholeNumberConvertedToString = numberWithoutFractionalPart.toString()
const numberSplittedByPlaceValuesButInReverse = [...EMPTY_ARRAY]
for (let forLoopCurrentDigitPositionIndex = wholeNumberConvertedToString.length; forLoopCurrentDigitPositionIndex > NUMBER_ZERO_IN_BASE_10; forLoopCurrentDigitPositionIndex -= PLACE_VALUE_LENGTH) {
numberSplittedByPlaceValuesButInReverse.push(
wholeNumberConvertedToString.slice(
forLoopCurrentDigitPositionIndex - PLACE_VALUE_LENGTH,
forLoopCurrentDigitPositionIndex
)
)
}
const numberSplittedByPlaceValuesInRightOrder = numberSplittedByPlaceValuesButInReverse.reverse()
const splittedNumberJoinedWithDots = numberSplittedByPlaceValuesInRightOrder.join(DOT_CHARACTER_AS_STRING)
return splittedNumberJoinedWithDots
}
export default formatNumberBySplittingPlaceValuesWithDots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment