Phone number
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"authors": [
|
||||
"masters3d"
|
||||
],
|
||||
"contributors": [
|
||||
"ffflorian",
|
||||
"lukaszklis",
|
||||
"SleeplessByte",
|
||||
"snowfrogdev"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"phone-number.ts"
|
||||
],
|
||||
"test": [
|
||||
"phone-number.test.ts"
|
||||
],
|
||||
"example": [
|
||||
".meta/proof.ci.ts"
|
||||
]
|
||||
},
|
||||
"blurb": "Clean up user-entered phone numbers so that they can be sent SMS messages.",
|
||||
"custom": {
|
||||
"version.tests.compatibility": "jest-29",
|
||||
"flag.tests.task-per-describe": false,
|
||||
"flag.tests.may-run-long": false,
|
||||
"flag.tests.includes-optional": false,
|
||||
"flag.tests.jest": true,
|
||||
"flag.tests.tstyche": false
|
||||
},
|
||||
"source": "Event Manager by JumpstartLab",
|
||||
"source_url": "http://tutorials.jumpstartlab.com/projects/eventmanager.html"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"track":"typescript","exercise":"phone-number","id":"fe0bf622176b487299f39b80ec5d6cce","url":"https://exercism.org/tracks/typescript/exercises/phone-number","handle":"briemens","is_requester":true,"auto_approve":false}
|
||||
+16683
File diff suppressed because one or more lines are too long
Generated
+2126
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"arcanis.vscode-zipfs",
|
||||
"dbaeumer.vscode-eslint",
|
||||
"esbenp.prettier-vscode"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"cSpell.words": ["exercism"],
|
||||
"search.exclude": {
|
||||
"**/.yarn": true,
|
||||
"**/.pnp.*": true
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
compressionLevel: mixed
|
||||
|
||||
enableGlobalCache: true
|
||||
@@ -0,0 +1,50 @@
|
||||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Before trying to execute the tests, ensure the assignment folder is set-up correctly by following the installation steps, namely `corepack yarn install` and the Editor SDK setup.
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ corepack yarn test
|
||||
```
|
||||
|
||||
## Skipped tests
|
||||
|
||||
In the test suites all tests but the first have been skipped.
|
||||
|
||||
Once you get a test passing, you can enable the next one by changing `xit` to `it`.
|
||||
Additionally tests may be grouped using `xdescribe`.
|
||||
Enable the group by changing that to `describe`.
|
||||
Finally, some exercises may have optional tests `it.skip`.
|
||||
Remove `.skip` to execute the optional test.
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit phone-number.ts` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [TypeScript track's documentation](https://exercism.org/docs/tracks/typescript)
|
||||
- The [TypeScript track's programming category on the forum](https://forum.exercism.org/c/programming/typescript)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
To get help if you're having trouble, you can use one of the following resources:
|
||||
|
||||
- [TypeScript QuickStart](https://www.typescriptlang.org/docs/handbook/release-notes/overview.html)
|
||||
- [ECMAScript 2015 Language Specification](https://www.ecma-international.org/wp-content/uploads/ECMA-262_6th_edition_june_2015.pdf) (pdf)
|
||||
- [Mozilla JavaScript Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)
|
||||
- [/r/typescript](https://www.reddit.com/r/typescript) is the TypeScript subreddit.
|
||||
- [StackOverflow](https://stackoverflow.com/questions/tagged/typescript) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||
@@ -0,0 +1,56 @@
|
||||
# Phone Number
|
||||
|
||||
Welcome to Phone Number on Exercism's TypeScript Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Clean up user-entered phone numbers so that they can be sent SMS messages.
|
||||
|
||||
The **North American Numbering Plan (NANP)** is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda.
|
||||
All NANP-countries share the same international country code: `1`.
|
||||
|
||||
NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as _area code_, followed by a seven-digit local number.
|
||||
The first three digits of the local number represent the _exchange code_, followed by the unique four-digit number which is the _subscriber number_.
|
||||
|
||||
The format is usually represented as
|
||||
|
||||
```text
|
||||
NXX NXX-XXXX
|
||||
```
|
||||
|
||||
where `N` is any digit from 2 through 9 and `X` is any digit from 0 through 9.
|
||||
|
||||
Sometimes they also have the country code (represented as `1` or `+1`) prefixed.
|
||||
|
||||
Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code if present.
|
||||
|
||||
For example, the inputs
|
||||
|
||||
- `+1 (613)-995-0253`
|
||||
- `613-995-0253`
|
||||
- `1 613 995 0253`
|
||||
- `613.995.0253`
|
||||
|
||||
should all produce the output
|
||||
|
||||
`6139950253`
|
||||
|
||||
**Note:** As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @masters3d
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ffflorian
|
||||
- @lukaszklis
|
||||
- @SleeplessByte
|
||||
- @snowfrogdev
|
||||
|
||||
### Based on
|
||||
|
||||
Event Manager by JumpstartLab - http://tutorials.jumpstartlab.com/projects/eventmanager.html
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
presets: [[require('@exercism/babel-preset-typescript'), { corejs: '3.38' }]],
|
||||
plugins: [],
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// @ts-check
|
||||
|
||||
import tsEslint from 'typescript-eslint'
|
||||
import config from '@exercism/eslint-config-typescript'
|
||||
import maintainersConfig from '@exercism/eslint-config-typescript/maintainers.mjs'
|
||||
|
||||
export default [
|
||||
...tsEslint.config(...config, {
|
||||
files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
|
||||
extends: maintainersConfig,
|
||||
}),
|
||||
{
|
||||
ignores: [
|
||||
// # Protected or generated
|
||||
'.git/**/*',
|
||||
'.vscode/**/*',
|
||||
|
||||
//# When using npm
|
||||
'node_modules/**/*',
|
||||
|
||||
// # Configuration files
|
||||
'babel.config.cjs',
|
||||
'jest.config.cjs',
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,22 @@
|
||||
module.exports = {
|
||||
verbose: true,
|
||||
projects: ['<rootDir>'],
|
||||
testMatch: [
|
||||
'**/__tests__/**/*.[jt]s?(x)',
|
||||
'**/test/**/*.[jt]s?(x)',
|
||||
'**/?(*.)+(spec|test).[jt]s?(x)',
|
||||
],
|
||||
testPathIgnorePatterns: [
|
||||
'/(?:production_)?node_modules/',
|
||||
'.d.ts$',
|
||||
'<rootDir>/test/fixtures',
|
||||
'<rootDir>/test/helpers',
|
||||
'__mocks__',
|
||||
],
|
||||
transform: {
|
||||
'^.+\\.[jt]sx?$': 'babel-jest',
|
||||
},
|
||||
moduleNameMapper: {
|
||||
'^(\\.\\/.+)\\.js$': '$1',
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@exercism/typescript-phone-number",
|
||||
"version": "1.0.0",
|
||||
"description": "Exercism exercises in Typescript.",
|
||||
"private": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/exercism/typescript"
|
||||
},
|
||||
"type": "module",
|
||||
"engines": {
|
||||
"node": "^18.16.0 || >=20.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@exercism/babel-preset-typescript": "^0.6.0",
|
||||
"@exercism/eslint-config-typescript": "^0.8.0",
|
||||
"@jest/globals": "^29.7.0",
|
||||
"@types/node": "~22.7.6",
|
||||
"babel-jest": "^29.7.0",
|
||||
"core-js": "~3.38.1",
|
||||
"eslint": "^9.12.0",
|
||||
"expect": "^29.7.0",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.5.3",
|
||||
"tstyche": "^2.1.1",
|
||||
"typescript": "~5.6.3",
|
||||
"typescript-eslint": "^8.10.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "corepack yarn node test-runner.mjs",
|
||||
"test:types": "corepack yarn tstyche",
|
||||
"test:implementation": "corepack yarn jest --no-cache --passWithNoTests",
|
||||
"lint": "corepack yarn lint:types && corepack yarn lint:ci",
|
||||
"lint:types": "corepack yarn tsc --noEmit -p .",
|
||||
"lint:ci": "corepack yarn eslint . --ext .tsx,.ts"
|
||||
},
|
||||
"packageManager": "yarn@4.5.1"
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import { describe, it, expect, xit } from '@jest/globals'
|
||||
import { clean } from './phone-number.ts'
|
||||
|
||||
describe('Phone Number', () => {
|
||||
describe('Cleanup user-entered phone numbers', () => {
|
||||
it('cleans the number', () => {
|
||||
expect(clean('(223) 456-7890')).toEqual('2234567890')
|
||||
})
|
||||
|
||||
it('cleans numbers with dots', () => {
|
||||
expect(clean('223.456.7890')).toEqual('2234567890')
|
||||
})
|
||||
|
||||
it('cleans numbers with multiple spaces', () => {
|
||||
expect(clean('223 456 7890 ')).toEqual('2234567890')
|
||||
})
|
||||
|
||||
it('invalid when 9 digits', () => {
|
||||
expect(() => clean('123456789')).toThrow(
|
||||
new Error('Incorrect number of digits')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid when 11 digits does not start with a 1', () => {
|
||||
expect(() => clean('22234567890')).toThrow(
|
||||
new Error('11 digits must start with 1')
|
||||
)
|
||||
})
|
||||
|
||||
it('valid when 11 digits and starting with 1', () => {
|
||||
expect(clean('12234567890')).toEqual('2234567890')
|
||||
})
|
||||
|
||||
it('valid when 11 digits and starting with 1 even with punctuation', () => {
|
||||
expect(clean('+1 (223) 456-7890')).toEqual('2234567890')
|
||||
})
|
||||
|
||||
it('invalid when more than 11 digits', () => {
|
||||
expect(() => clean('321234567890')).toThrow(
|
||||
new Error('More than 11 digits')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid with letters', () => {
|
||||
expect(() => clean('123-abc-7890')).toThrow(
|
||||
new Error('Letters not permitted')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid with punctuations', () => {
|
||||
expect(() => clean('123-@:!-7890')).toThrow(
|
||||
new Error('Punctuations not permitted')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if area code starts with 0', () => {
|
||||
expect(() => clean('(023) 456-7890')).toThrow(
|
||||
new Error('Area code cannot start with zero')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if area code starts with 1', () => {
|
||||
expect(() => clean('(123) 456-7890')).toThrow(
|
||||
new Error('Area code cannot start with one')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if exchange code starts with 0', () => {
|
||||
expect(() => clean('(223) 056-7890')).toThrow(
|
||||
new Error('Exchange code cannot start with zero')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if exchange code starts with 1', () => {
|
||||
expect(() => clean('(223) 156-7890')).toThrow(
|
||||
new Error('Exchange code cannot start with one')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if area code starts with 0 on valid 11-digit number', () => {
|
||||
expect(() => clean('1 (023) 456-7890')).toThrow(
|
||||
new Error('Area code cannot start with zero')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if area code starts with 1 on valid 11-digit number', () => {
|
||||
expect(() => clean('1 (123) 456-7890')).toThrow(
|
||||
new Error('Area code cannot start with one')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if exchange code starts with 0 on valid 11-digit number', () => {
|
||||
expect(() => clean('1 (223) 056-7890')).toThrow(
|
||||
new Error('Exchange code cannot start with zero')
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid if exchange code starts with 1 on valid 11-digit number', () => {
|
||||
expect(() => clean('1 (223) 156-7890')).toThrow(
|
||||
new Error('Exchange code cannot start with one')
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,33 @@
|
||||
const hasLetters = (value: string) => Boolean(value.match(/[a-zA-Z]/))
|
||||
const hasPunctuations = (value: string) => Boolean(value.match(/[^a-zA-Z0-9\(\)\-\ .+]/))
|
||||
const getNumbersOnly = (value: string) => value.replace(/\D/g, '')
|
||||
const parsePhoneNumber = (value: string) =>
|
||||
(getNumbersOnly(value).match(/(\d{0,1})\ {0,1}(\d{3})(\d{7})$/) || []).slice(1)
|
||||
|
||||
export const clean = (phoneNumber: string): string => {
|
||||
const [countryCode, areaCode, exchangeCode] = parsePhoneNumber(phoneNumber)
|
||||
|
||||
if (hasLetters(phoneNumber)) {
|
||||
throw new Error('Letters not permitted')
|
||||
} else if (hasPunctuations(phoneNumber)) {
|
||||
throw new Error('Punctuations not permitted')
|
||||
} else if (getNumbersOnly(phoneNumber).length > 11) {
|
||||
throw new Error('More than 11 digits')
|
||||
} else if (exchangeCode === undefined || areaCode === undefined) {
|
||||
throw new Error('Incorrect number of digits')
|
||||
} else if (countryCode && countryCode === '2') {
|
||||
throw new Error('11 digits must start with 1')
|
||||
} else if (countryCode && countryCode !== '1') {
|
||||
throw new Error('More than 11 digits')
|
||||
} else if (areaCode?.[0] === '0') {
|
||||
throw new Error('Area code cannot start with zero')
|
||||
} else if (areaCode?.[0] === '1') {
|
||||
throw new Error('Area code cannot start with one')
|
||||
} else if (exchangeCode === undefined || exchangeCode?.[0] === '0') {
|
||||
throw new Error('Exchange code cannot start with zero')
|
||||
} else if (exchangeCode?.[0] === '1') {
|
||||
throw new Error('Exchange code cannot start with one')
|
||||
}
|
||||
|
||||
return `${areaCode}${exchangeCode}`
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 👋🏽 Hello there reader,
|
||||
*
|
||||
* It looks like you are working on this solution using the Exercism CLI and
|
||||
* not the online editor. That's great! The file you are looking at executes
|
||||
* the various steps the online test-runner also takes.
|
||||
*
|
||||
* @see https://github.com/exercism/typescript-test-runner
|
||||
*
|
||||
* TypeScript track exercises generally consist of at least two out of three
|
||||
* types of tests to run.
|
||||
*
|
||||
* 1. tsc, the TypeScript compiler. This tests if the TypeScript code is valid
|
||||
* 2. tstyche, static analysis tests to see if the types used are expected
|
||||
* 3. jest, runtime implementation tests to see if the solution is correct
|
||||
*
|
||||
* If one of these three fails, this script terminates with -1, -2, or -3
|
||||
* respectively. If it succeeds, it terminates with exit code 0.
|
||||
*
|
||||
* @note you need corepack (bundled with node LTS) enabled in order for this
|
||||
* test runner to work as expected. Follow the installation and test
|
||||
* instructions if you see errors about corepack or pnp.
|
||||
*/
|
||||
|
||||
import { execSync } from 'node:child_process'
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import { exit } from 'node:process'
|
||||
import { URL } from 'node:url'
|
||||
|
||||
/**
|
||||
* Before executing any tests, the test runner attempts to find the
|
||||
* exercise config.json file which has metadata about which types of tests
|
||||
* to run for this solution.
|
||||
*/
|
||||
const metaDirectory = new URL('./.meta/', import.meta.url)
|
||||
const exercismDirectory = new URL('./.exercism/', import.meta.url)
|
||||
const configDirectory = existsSync(metaDirectory)
|
||||
? metaDirectory
|
||||
: existsSync(exercismDirectory)
|
||||
? exercismDirectory
|
||||
: null
|
||||
|
||||
if (configDirectory === null) {
|
||||
throw new Error(
|
||||
'Expected .meta or .exercism directory to exist, but I cannot find it.'
|
||||
)
|
||||
}
|
||||
|
||||
const configFile = new URL('./config.json', configDirectory)
|
||||
if (!existsSync(configFile)) {
|
||||
throw new Error('Expected config.json to exist at ' + configFile.toString())
|
||||
}
|
||||
|
||||
// Experimental: import config from './config.json' with { type: 'json' }
|
||||
/** @type {import('./config.json') } */
|
||||
const config = JSON.parse(readFileSync(configFile))
|
||||
|
||||
const jest = !config.custom || config.custom['flag.tests.jest']
|
||||
const tstyche = config.custom?.['flag.tests.tstyche']
|
||||
console.log(
|
||||
`[tests] tsc: ✅, tstyche: ${tstyche ? '✅' : '❌'}, jest: ${jest ? '✅' : '❌'}, `
|
||||
)
|
||||
|
||||
/**
|
||||
* 1. tsc: the typescript compiler
|
||||
*/
|
||||
try {
|
||||
console.log('[tests] tsc (compile)')
|
||||
execSync('corepack yarn lint:types', {
|
||||
stdio: 'inherit',
|
||||
cwd: process.cwd(),
|
||||
})
|
||||
} catch {
|
||||
exit(-1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 2. tstyche: type tests
|
||||
*/
|
||||
if (tstyche) {
|
||||
try {
|
||||
console.log('[tests] tstyche (type tests)')
|
||||
execSync('corepack yarn test:types', {
|
||||
stdio: 'inherit',
|
||||
cwd: process.cwd(),
|
||||
})
|
||||
} catch {
|
||||
exit(-2)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 3. jest: implementation tests
|
||||
*/
|
||||
if (jest) {
|
||||
try {
|
||||
console.log('[tests] tstyche (implementation tests)')
|
||||
execSync('corepack yarn test:implementation', {
|
||||
stdio: 'inherit',
|
||||
cwd: process.cwd(),
|
||||
})
|
||||
} catch {
|
||||
exit(-3)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Done! 🥳
|
||||
*/
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"display": "Configuration for Exercism TypeScript Exercises",
|
||||
"compilerOptions": {
|
||||
// Allows you to use the newest syntax, and have access to console.log
|
||||
// https://www.typescriptlang.org/tsconfig#lib
|
||||
"lib": ["ES2020", "dom"],
|
||||
// Make sure typescript is configured to output ESM
|
||||
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm
|
||||
"module": "Node16",
|
||||
// Since this project is using babel, TypeScript may target something very
|
||||
// high, and babel will make sure it runs on your local Node version.
|
||||
// https://babeljs.io/docs/en/
|
||||
"target": "ES2020", // ESLint doesn't support this yet: "es2022",
|
||||
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
|
||||
// Because jest-resolve isn't like node resolve, the absolute path must be .ts
|
||||
"allowImportingTsExtensions": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Because we'll be using babel: ensure that Babel can safely transpile
|
||||
// files in the TypeScript project.
|
||||
//
|
||||
// https://babeljs.io/docs/en/babel-plugin-transform-typescript/#caveats
|
||||
"isolatedModules": true
|
||||
},
|
||||
"include": [
|
||||
"*.ts",
|
||||
"*.tsx",
|
||||
".meta/*.ts",
|
||||
".meta/*.tsx",
|
||||
"__typetests__/*.tst.ts"
|
||||
],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user