Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: sort data alphabetically #836

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions scripts/sort_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import re
from sys import argv

def sort_all_arrays_in_file(filename):
with open(filename, 'r') as file:
content = file.read()

array_pattern = re.compile(r'std::to_array<std::string_view>\(\{([\s\S]*?)\}\);')
matches = array_pattern.findall(content)

if not matches:
print(f"No std::to_array<std::string_view> arrays found in {filename}.")
return

for match in matches:
elements = re.split(r',\s*(?=")', match.strip())

elements = [element.strip().replace(",", "") for element in elements if element.strip()]

sorted_elements = sorted(elements, key=lambda x: x.strip('"'))

sorted_array = ',\n '.join(sorted_elements)
sorted_array += ","
sorted_array = f'std::to_array<std::string_view>({{\n {sorted_array}\n}});'

old_array_pattern = re.escape(f'std::to_array<std::string_view>({{{match}}});')
content = re.sub(old_array_pattern, sorted_array, content, count=1)

with open(filename, 'w') as file:
file.write(content)

print(f"Sorted arrays in {filename} successfully.")

def sort_all_arrays_in_directory(directory):
for filename in os.listdir(directory):
if filename.endswith('_data.h') and filename not in ["commerce_data.h", "person_data.h"]:
filepath = os.path.join(directory, filename)
sort_all_arrays_in_file(filepath)

if len(argv) > 1:
directory = argv[1]
sort_all_arrays_in_directory(directory)
else:
print("No directory specified, please pass in directory as command line argument")
4 changes: 3 additions & 1 deletion src/modules/airline_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
namespace faker::airline
{
const auto aircraftTypes = std::to_array<std::string_view>({
"regional", "narrowbody", "widebody"
"narrowbody",
"regional",
"widebody",
});

const auto airlines = std::to_array<AirlineInfo>({
Expand Down
Loading
Loading