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

feat: add Ja locale to Person #386

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

ymtszw
Copy link

@ymtszw ymtszw commented Nov 1, 2020

I've added:

  • USAGE.md docs if applicable
  • CHANGELOG.md

Hi! Thanks for maintaining Faker package!
I'm @ymtszw from Tokyo, Japan. This is (probably) the first attempt
to introduce Ja locale to Faker, starting with Person!

As noted in moduledoc, samples are borrowed from faker-ruby/faker,
which itself is loosely built upon population ranking of recent
family names in Japan, and "polularity" ranking of given names
for recent newborns.

One thing to note: in Japanese, names can be read various ways
even they share the same "kanji" notations (heteronyms).
To disambiguate, it is common to put phonetic notations in "kana"
(either in "hiragana" or "katakana") alongsite with kanji notations
in legal documents or web applications.
This patch includes the feature for that. name/0 and other common
functions produces only kanji notations, but *_with_kana variants
produces tuples accompanied by hiragana and katakana notations.

To support this feature, I had slightly refactored sampler/2 macro,
allowing sample list to host non-literals.

Hope this is acceptable. Thanks in advance!

Comment on lines -147 to +151
count = Enum.count(data)

mapped_data =
data |> Enum.with_index() |> Enum.into(%{}, fn {k, v} -> {v, k} end) |> Macro.escape()

quote do
@count Enum.count(unquote(data))
@mapped_data unquote(data) |> Enum.with_index() |> Enum.into(%{}, fn {k, v} -> {v, k} end)
def unquote(name)() do
unquote(mapped_data)
|> Map.get(Faker.random_between(0, unquote(count - 1)))
Map.get(@mapped_data, Faker.random_between(0, @count - 1))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the modification mentioned in the OP.
Previously count and mapped_data calculations are applied to AST outside of quote, which did not allow non-literals (such as tuples) to be passed to data.
Pushing it inside quote makes them expanded at compilation of caller modules, so any expressions in data are properly evaluated just like other module attributes or other "outside the def" expressions,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants