Skip to content

Commit

Permalink
Write code (from python)
Browse files Browse the repository at this point in the history
Signed-off-by: alumag <10659903+alumag@users.noreply.github.com>
  • Loading branch information
alumag committed Jan 13, 2023
1 parent e1c6c79 commit e26a091
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 0 deletions.
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
4 changes: 4 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Authors
--------

* [Aluma Gelbard](https://github.com/alumag)
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright © 2023 Aluma Gelbard

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# hebrew-names

Random Hebrew name generator

## Usage

This module is available on npm:

```bash
npm install hebrew-names
```

## Usage

* `Ethnicity` [optional]: jew, muslim, christian, druze or other

* `Gender` [optional]: male or female

* Generate random first names:
```js
>>> const { getFirstName } = require('hebrew-names');

>>> getFirstName('jew', 'female');
'יעל'
````

* Generate random last names:
```js
>>> const { getLastName } = require('hebrew-names');
>>> getLastName('jew');
'גלברד'
```

* Generate random full names:
```js
>>> const { getFullName } = require('hebrew-names');
>> getFullName('jew', 'male');
'משה כהן'
```

## License

This project is released under an `MIT License`

Data in the following files are public domain (derived from Israel's Central Bureau of Statistics 1948-2021 data):
- data/jew.male.first
- data/jew.female.first
- data/jew.last
- data/muslim.male.first
- data/muslim.female.first
- data/muslim.last
- data/christian.male.first
- data/christian.female.first
- data/christian.last
- data/druze.male.first
- data/druze.female.first
- data/druze.last
- data/other.male.first
- data/other.female.first
- data/other.last
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type Ethnicity = 'jew' | 'muslim' | 'christian' | 'druze' | 'other';

export type Gender = 'male' | 'female';

export function getFirstName(ethnicity?: Ethnicity, gender?: Gender): string;

export function getLastName(ethnicity?: Ethnicity): string;

export function getFullName(ethnicity?: Ethnicity, gender?: Gender): string;
98 changes: 98 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const fs = require('fs');
const path = require('path');

const ETHNICITIES = ['jew', 'muslim', 'christian', 'druze', 'other'];
const GENDERS = ['male', 'female'];

const getFilePath = filename => path.join(__dirname, 'data', filename);

const FILES = {
'first:jew:male': getFilePath('jew.male.first'),
'first:jew:female': getFilePath('jew.female.first'),
'first:muslim:male': getFilePath('muslim.male.first'),
'first:muslim:female': getFilePath('muslim.female.first'),
'first:christian:male': getFilePath('christian.male.first'),
'first:christian:female': getFilePath('christian.female.first'),
'first:druze:male': getFilePath('druze.male.first'),
'first:druze:female': getFilePath('druze.female.first'),
'first:other:male': getFilePath('other.male.first'),
'first:other:female': getFilePath('other.female.first'),
'last:jew': getFilePath('jew.last'),
'last:muslim': getFilePath('muslim.last'),
'last:christian': getFilePath('christian.last'),
'last:druze': getFilePath('druze.last'),
'last:other': getFilePath('other.last'),
}

const CUMULATIVES = {
'first:jew:male': 100,
'first:jew:female': 100,
'first:muslim:male': 100,
'first:muslim:female': 100,
'first:christian:male': 100,
'first:christian:female': 100,
'first:druze:male': 100,
'first:druze:female': 100,
'first:other:male': 100,
'first:other:female': 100,
'last:jew': 87.81657841999986,
'last:muslim': 93.36219688600355,
'last:christian': 87.11965461299987,
'last:druze': 95.52368873200001,
'last:other': -1,
}

function choose(choices) {
var index = Math.floor(Math.random() * choices.length);
return choices[index];
}

function selectEthnicity(ethnicity) {
ethnicity = ethnicity ?? choose(ETHNICITIES);
if (!ETHNICITIES.includes(ethnicity)) {
throw new TypeError(`${ethnicity} is not supported as ethnicity. Valid ethnicities: ${ETHNICITIES}`);
}
return ethnicity;
}

function selectGender(gender) {
gender = gender ?? choose(GENDERS);
if (!GENDERS.includes(gender)) {
throw new TypeError(`${gender} is not supported as gender. Valid genders: ${GENDERS}`);
}
return gender;
}

function getName(filename) {
const selected = Math.random() * CUMULATIVES[filename];
const lines = fs.readFileSync(FILES[filename], 'utf16le').split('\n');
for (const line of lines) {
const [name, , , cumulative, ] = line.split('\t');
if (Number(cumulative) >= selected) {
return name
}
}
return "";
}

function getFirstName(ethnicity, gender) {
ethnicity = selectEthnicity(ethnicity);
gender = selectGender(gender);
return getName(`first:${ethnicity}:${gender}`);
}

function getLastName(ethnicity) {
ethnicity = selectEthnicity(ethnicity);
return getName(`last:${ethnicity}`);
}

function getFullName(ethnicity, gender) {
ethnicity = selectEthnicity(ethnicity);
return `${getFirstName(ethnicity, gender)} ${getLastName(ethnicity)}`.trim();
}

module.exports = {
getFirstName,
getLastName,
getFullName
};
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "hebrew-names",
"version": "1.0.0",
"description": "Random Hebrew name generator",
"files": [
"README.md",
"AUTHORS.md",
"index.js",
"data/*"
],
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "index.spec.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alumag/hebrew-names-js.git"
},
"keywords": [
"hebrew"
],
"author": "Aluma Gelbard",
"license": "MIT",
"bugs": {
"url": "https://github.com/alumag/hebrew-names-js/issues"
},
"homepage": "https://github.com/alumag/hebrew-names-js#readme"
}

0 comments on commit e26a091

Please sign in to comment.