diff --git a/README.rst b/README.rst index a1006ad..de150e0 100644 --- a/README.rst +++ b/README.rst @@ -83,12 +83,12 @@ Providers TODO list. - [ ] base -- [X] faker.provider.address +- [o] faker.provider.address - [ ] faker.provider.automotive -- [ ] faker.provider.bank +- [o] faker.provider.bank - [ ] faker.provider.barcode - [ ] faker.provider.color -- [X] faker.provider.company +- [o] faker.provider.company - [ ] faker.provider.credit_card - [ ] faker.provider.currency - [ ] faker.provider.date_time @@ -96,15 +96,15 @@ TODO list. - [ ] faker.provider.geo - [ ] faker.provider.internet - [ ] faker.provider.isbn -- [X] faker.provider.job +- [o] faker.provider.job - [ ] faker.provider.lorem - [ ] faker.provider.misc -- [X] faker.provider.person -- [X] faker.provider.phone_number +- [o] faker.provider.person +- [o] faker.provider.phone_number - [ ] faker.provider.profile - [ ] faker.provider.python - [ ] faker.provider.ssn -- [X] faker.provider.user_agent +- [o] faker.provider.user_agent ************ API document diff --git a/faker.nimble b/faker.nimble index 0542814..e6ed512 100644 --- a/faker.nimble +++ b/faker.nimble @@ -10,6 +10,7 @@ bin = @["faker/cli/faker"] binDir = "bin" from os import `/`, splitFile +from algorithm import sort import strformat, strutils, sequtils # Dependencies @@ -19,6 +20,13 @@ requires "nim >= 1.0.0" let providerDir = "src" / "faker" / "provider" +proc addGeneratedText(lines: var seq[string]) = + lines.add("# ----------------------------------------------- #") + lines.add("# This module was generated by 'nimble genProvs'. #") + lines.add("# See 'faker.nimble'. #") + lines.add("# ----------------------------------------------- #") + lines.add("") + task docs, "Generate API documents": exec "nimble doc --index:on --project --out:docs --hints:off src/faker.nim" @@ -56,19 +64,17 @@ task genMod, "Generate module": echo "Generated: " & newPath task genProvs, "Generate provider file": + var providers: seq[string] for dir in listDirs(providerDir): let dstFile = dir & ".nim" var lines: seq[string] - lines.add("# ----------------------------------------------- #") - lines.add("# This module was generated by 'nimble genProvs'. #") - lines.add("# See 'faker.nimble'. #") - lines.add("# ----------------------------------------------- #") - lines.add("") + lines.addGeneratedText() lines.add("import util") lines.add("import ../base") # Get submodule names let (_, prefix, _) = splitFile(dir) + providers.add(prefix) var modules: seq[string] var locales: seq[string] # en_US, ja_JP for file in listFiles(dir): @@ -103,3 +109,13 @@ task genProvs, "Generate provider file": let body = lines.join("\n") writeFile(dir & ".nim", body) + providers.sort() + + # generate provider.nim + var lines: seq[string] + let prov = providers.join(", ") + lines.addGeneratedText() + lines.add(&"import provider/[{prov}]") + lines.add(&"export {prov}") + let body = lines.join("\n") + writeFile(providerDir & ".nim", body) diff --git a/src/faker/provider.nim b/src/faker/provider.nim index 4f31505..2075aee 100644 --- a/src/faker/provider.nim +++ b/src/faker/provider.nim @@ -1,2 +1,7 @@ -import provider/[address, phone_number, job, person, company, user_agent] -export address, phone_number, job, person, company, user_agent +# ----------------------------------------------- # +# This module was generated by 'nimble genProvs'. # +# See 'faker.nimble'. # +# ----------------------------------------------- # + +import provider/[address, bank, company, job, person, phone_number, user_agent] +export address, bank, company, job, person, phone_number, user_agent \ No newline at end of file diff --git a/src/faker/provider/bank.nim b/src/faker/provider/bank.nim new file mode 100644 index 0000000..e2ba708 --- /dev/null +++ b/src/faker/provider/bank.nim @@ -0,0 +1,18 @@ +# ----------------------------------------------- # +# This module was generated by 'nimble genProvs'. # +# See 'faker.nimble'. # +# ----------------------------------------------- # + +import util +import ../base +import bank/[bank_en_US] +export base + +genProc bank, + [ + bankCountry, + bban, + iban, + ], [ + en_US, + ] \ No newline at end of file diff --git a/src/faker/provider/bank/bank_en_US.nim b/src/faker/provider/bank/bank_en_US.nim new file mode 100644 index 0000000..7fdd372 --- /dev/null +++ b/src/faker/provider/bank/bank_en_US.nim @@ -0,0 +1,33 @@ +include interfaces + +import tables +from strutils import join, parseBiggestUInt +from strformat import `&` +from sequtils import mapIt + +const + countryCode = "GB" + bbanFormat = "#############" + asciiUpperCase = {'A'..'Z'} + +let + alpha = block: + var tbl = initTable[char, string]() + for ch in asciiUpperCase: + tbl.add(ch, $(ch.ord mod 55)) + tbl + +proc bankCountry*(f: Faker): string = + countryCode + +proc bban*(f: Faker): string = + var r = f.rand + for i in 0..3: + result.add(r.sample(asciiUpperCase)) + result.add(r.formatNumbers(bbanFormat)) + +proc iban*(f: Faker): string = + let bban = f.bban() + let checkInt = 98 - (f.rand.rand(1..100) mod 97) + let check = &"{checkInt:>02}" + result = countryCode & check & bban diff --git a/src/faker/provider/bank/interfaces.nim b/src/faker/provider/bank/interfaces.nim new file mode 100644 index 0000000..993bbf9 --- /dev/null +++ b/src/faker/provider/bank/interfaces.nim @@ -0,0 +1,14 @@ +# interfaces はプロシージャの定義のみをまとめたモジュール。 +# C言語でいうヘッダファイルのように使用する。 +# +# このモジュールをincludeしたモジュールは、このモジュールに定義しているプロシー +# ジャを全て実装しないと、コンパイルが通らない。 + +import random +import ../../base +import ../util +export base + +proc bankCountry*(f: Faker): string +proc bban*(f: Faker): string +proc iban*(f: Faker): string diff --git a/tests/test1.nim b/tests/test1.nim index c635bc7..2bb87d0 100644 --- a/tests/test1.nim +++ b/tests/test1.nim @@ -24,4 +24,7 @@ suite "basic usage": echo fake.opera() echo fake.safari() echo fake.internetExplorer() + echo fake.bankCountry() + echo fake.bban() + echo fake.iban() echo "-----"