zortt
🧩 Syntax:
import re
import os
def objc_to_ts_type(objc_type):
# Basic mapping, extend as necessary
mapping = {
"NSInteger": "number",
"NSUInteger": "number",
"BOOL": "boolean",
"NSString": "string",
"CLLocation": "any", # Need a specific TS interface for this
}
return mapping.get(objc_type, "any") # Default to 'any'
def parse_property(line):
match = re.search(r"@property\s*\(.+\)\s*([^*]*\*?)\s*([^;]*);", line)
if match:
return match.group(2), objc_to_ts_type(match.group(1).strip())
return None, None
def parse_file(filename):
interfaces = {}
current_interface = None
with open(filename, 'r') as file:
for line in file:
if "@interface" in line:
current_interface = re.search(r"@interface\s*([^:]*):", line).group(1)
interfaces[current_interface] = {}
elif "@property" in line and current_interface:
prop_name, prop_type = parse_property(line)
if prop_name:
interfaces[current_interface][prop_name] = prop_type
elif "@end" in line:
current_interface = None
return interfaces
def write_ts_interfaces(interfaces, output_file):
with open(output_file, 'w') as file:
for interface_name, properties in interfaces.items():
file.write(f"interface {interface_name} {{\n")
for prop_name, prop_type in properties.items():
file.write(f" {prop_name}: {prop_type};\n")
file.write("}\n\n")
def main():
input_dir = '/path/to/your/h/files'
output_file = '/path/to/your/output.ts'
all_interfaces = {}
for filename in os.listdir(input_dir):
if filename.endswith('.h'):
interfaces = parse_file(os.path.join(input_dir, filename))
all_interfaces.update(interfaces)
write_ts_interfaces(all_interfaces, output_file)
if __name__ == "__main__":
main()