Sub-Super Script and Roman numerals Library
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FFriZz

//@version=5

// ███████╗███████╗███████ ██ ███████ ███████╗  
// ██╔════▒██╔════▒██╔══██╗██░╚════██░╚════██░  
// █████╗ ░█████╗ ░██████╔╝██░  ███╔═╝  ███╔═╝  
// ██╔══╝ ░██╔══╝ ░██╔══██╗██░██╔══╝  ██╔══╝    
// ██░░   ░██░░   ░██░░ ██░██░███████╗███████╗  
// ╚═╝░   ░╚═╝░   ░╚═╝░ ╚═╝╚═╝╚══════╝╚══════╝  Labz

// @description Library to transform numbers into Roman numerals / Super-Sub script / check if value is alpha or number
library("Sub_Super_Script_and_RomanNumerals_Library")

import FFriZz/FrizBug/9 as p

// @function check to see if value is a number
// @param input (string/float/int) value to check
// @returns (na) if value is NOT a number and input (string/float/int) if value is a number
export method isnumber(string input) =>
	numbers = '0123456789.,'
	out = ''
	if str.contains(input,' ')
		out := na
	else
		s_num = str.split(input,'')
		for i in s_num
			if str.contains(numbers,i)
				out := input
			else
				out := na
				break
	out
export method isnumber(float input) =>
	n = str.tostring(input).isnumber()
	out = 0.
	if not na(n)
		out := str.tonumber(n)
	out
export method isnumber(int input) =>
	n = str.tostring(input).isnumber()
	out = 0.
	if not na(n)
		out := str.tonumber(n)
	out
	


// @function check a string if it is alpha(doesnt contain numbers)
// @param string string - string to check if alpha
// @returns (string) if string input does NOT contain numbers, return (na) if input string contains numbers
export method isalpha(string input) =>
	out = ''
	num = isnumber(input)
	if na(num)
		out := input
	else
		out := na
	out
export method isalpha(float input) =>
	string = str.tostring(input)
	out = string.isalpha()
export method isalpha(int input) =>
	string = str.tostring(input)
	out = string.isalpha()




// @function convert a string's numbers from normal print to super-script [0⁰1¹2²3³4⁴5⁵6⁶7⁷8⁸9⁹]
// @param num (string/int/float) input value to transform
// @returns string of input with numbers converted to super-script
export method super(string num) =>
	supers = array.from('⁰','¹','²','³','⁴','⁵','⁶','⁷','⁸','⁹')
	out = str.tostring(num)
	for i = 0 to 9
		out := str.replace_all(out,str.tostring(i),supers.get(i))
	out
export method super(float num) =>
	out = str.tostring(num).super()
export method super(int num) =>
	out = str.tostring(num).super()



// @function convert a string's numbers from normal print to sub-script [0₀1₁2₂3₃4₄5₅6₆7₇8₈9₉]
// @param num (string/int/float) input value to transform
// @returns string of input with numbers converted to sub-script
export method sub(string num) =>
	subs = array.from('₀','₁','₂','₃','₄','₅','₆','₇','₈','₉')
	out = str.tostring(num)
	for i = 0 to 9
		out := str.replace_all(out,str.tostring(i),subs.get(i))
	out
export method sub(float num) =>
	out = sub(str.tostring(num))
export method sub(int num) =>
	out = sub(str.tostring(num))



// @function convert a string of numbers, float, or int 
// @param num (string) input number to transform
// @param trunc (bool | false) true to truncate float value, false to show roman numerals with decimals (XX.VI) 
// @returns string of roman numerals representing the input (num)
export method roman(string num,bool trunc = false) =>
    numbers = array.from( 1,    4,   5,    9,   10,  40,  50, 90,  100, 400,  500, 900, 1000, 4000, 5000, 9000, 10000, 40000, 50000, 90000,  100000)
    roman   = array.from("I", "IV", "V", "IX", "X", "XL","L","XC", "C", "CD", "D", "CM", "M", 'Mↁ', 'ↁ', 'Mↂ', 'ↂ', 'ↂↇ',  'ↇ', 'ↂↈ',   'ↈ')
	build   = ''
	build_d = ''
    
	if na(num.isalpha())
		a_split = str.split(str.replace_all(num,',',''),'.')
		for i = 0 to a_split.size()-1
			
			if trunc and i
				break	
			else if i
				build_d += '.'
			
			number  = str.tonumber(a_split.get(i))
			n = 20  
			while n >= 0
				div = number / numbers.get(n)
				number %= numbers.get(n)

				while div >= 1
					switch i
						0 => build += roman.get(n)
						1 => build_d += roman.get(n)
					div -= 1

				n -= 1

	build+build_d





// USAGE 
// Str = str.split(str.tostring(close),'.')
// if Str.size() == 1
// 	Str.push('00')
// CLOSE = str.tostring(close)
	
// p.print(roman(str.tostring(close)),'Close price - ROMAN NUMERAL',tick = true,pos='8')
// p.print('\nSUPER = '+Str.get(0)+'.'+super(Str.get(1))+'\n SUB ='+Str.get(0)+'.'+sub(Str.get(1)),'SUPER/SUB script',tick = true,pos = '1')
// p.print('\nfloat = '+CLOSE.isnumber()+'\nalpha ='+CLOSE.isalpha(),'Close price - isnumber/isalpha',tick = true,pos = '7')




