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

v4.1.0 #17

Merged
merged 27 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
32dd964
Update: description for breakdown to be more forward
dhershman1 Oct 13, 2024
f6dfb31
Update: Rename unit test files
dhershman1 Oct 13, 2024
b0b3b23
Added: New normalize function
dhershman1 Oct 13, 2024
77223f5
Added: validate function
dhershman1 Oct 13, 2024
952607c
Added: isValidWithFormat function
dhershman1 Oct 13, 2024
521c925
Fixed: Param order in docs
dhershman1 Oct 13, 2024
1ba7765
Removed: commented out test on uglify
dhershman1 Oct 13, 2024
19c4586
Added: Type definitions for new functions
dhershman1 Oct 13, 2024
b28807e
Fixed: Param naming
dhershman1 Oct 13, 2024
d653550
Added: findSeparators function
dhershman1 Oct 13, 2024
a904434
Fixed: wording in docs
dhershman1 Oct 13, 2024
0a3b9d9
Chore: cleanup types files
dhershman1 Oct 13, 2024
b4a2ce0
Added: Curried test and error test to isValidWithFormat
dhershman1 Oct 13, 2024
c289640
Added: error when format is not provided to isValidWithFormat
dhershman1 Oct 13, 2024
9037993
Updated: isValid Description
dhershman1 Oct 13, 2024
f6bb2c1
Update: Unit test description for format
dhershman1 Oct 13, 2024
bad9cc9
Fix: Keep tests consistent
dhershman1 Oct 13, 2024
bdcca85
Added: breakdownWithFormat function
dhershman1 Oct 13, 2024
52c1bd5
Fixed: Standarize testing
dhershman1 Oct 13, 2024
fdb6812
Updated: Strip out kyanite from isValid
dhershman1 Oct 13, 2024
5e85d46
Updated: Strip out kyanite from uglify
dhershman1 Oct 13, 2024
1a6c220
Updated: Strip out kyanite from format
dhershman1 Oct 13, 2024
1f4ec8f
Added: New internals for format to use
dhershman1 Oct 13, 2024
7be22d6
Updated: Dependency Updates and removed Kyanite
dhershman1 Oct 13, 2024
a6a1973
Fixed: Make sure all tests are ran not just format
dhershman1 Oct 13, 2024
027d04e
Fixed: Unneeded call to uglify
dhershman1 Oct 13, 2024
d06cd28
Update: Changelog
dhershman1 Oct 13, 2024
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
31 changes: 31 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Changelog

## v4.1.0

### New

- Added `normalize` function
- This function strips out special characters and trims the phone number, much like uglify but skips non-digit characters
- Example: `normalize('555.444.3333 x 123') // => '5554443333x123'` vs `uglify('555.444.3333 x 123') // => 5554443333123`
- Added `validate` function
- This is a validation function, but works better for world wide phone numbers as well. Expects the full number
- Example: `333-444-5555` comes back valid but `444-5555` is invalid to this function
- Added `isValidWithFormat` function
- This takes a string phone number and a format string and validates the phone using the format
- It's also passed through the `validate` function for an extra step of validation
- Added `findSeparators` function
- A simple function that finds the separators in a phone number and returns them as an array
- Added `breakdownWithFormat` function
- Works a lot like `breakdown` but follows a strict format provided by the user to breakdown the number into an object
- This allows for a wider range of phone number support for breakdown


### Changed

- `Phone-fns` is no longer dependant on `Kyanite` and is dependency free!
- `isValid` description to explain that it mostly focused on NANP numbers
- `breakdown` description to better explain that it's main focus is NANP numbers and its gachas
- We more than doubled our unit tests! Woo!

### Chore

- Renamed test files to `*.spec.js` instead of just `*.js`

## v4.0.2

### Fixed
Expand Down
75 changes: 31 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phone-fns",
"version": "4.0.2",
"version": "4.1.0",
"description": "A small, modern, and functional phone library for javascript",
"main": "dist/phone-fns.min.js",
"module": "src/index.js",
Expand All @@ -11,10 +11,10 @@
"scripts": {
"prepack": "npm-run-all --parallel docs scripts lint test --serial build",
"scripts": "node scripts/create-export.js",
"docs": "node_modules/.bin/jsdoc -c jsdoc.json",
"docs": "jsdoc -c jsdoc.json",
"build": "rollup -c",
"lint": "standard src/**/*.js",
"test": "tape tests/*.js | tap-on"
"test": "tape tests/*.spec.js | tap-on"
},
"exports": {
".": {
Expand All @@ -35,7 +35,8 @@
"standard": {
"ignore": [
"docs/*",
"dist/*"
"dist/*",
"types/*"
]
},
"repository": {
Expand Down Expand Up @@ -71,14 +72,11 @@
"globby": "13.2.2",
"jsdoc": "4.0.3",
"npm-run-all": "4.1.5",
"pinet": "1.1.5",
"pinet": "1.2.0",
"rollup": "4.24.0",
"rollup-plugin-filesize": "10.0.0",
"standard": "17.1.2",
"tap-on": "1.0.0",
"tape": "5.9.0"
},
"dependencies": {
"kyanite": "3.1.0"
}
}
9 changes: 9 additions & 0 deletions src/_internals/_hasPlaceholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @private
* @function
* @param {String} str The string to check for placeholders
* @returns {Boolean} Whether or not the string has a placeholder
*/
export default function _hasPlaceholder (str) {
return str.includes('_')
}
9 changes: 9 additions & 0 deletions src/_internals/_uglifyFormats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @private
* @function
* @param {String} str The string to strip special characters
* @returns {String} The newly created string with special characters stripped
*/
export default function _uglifyFormats (str) {
return str.replace(/[^a-wyz]/gi, '')
}
2 changes: 1 addition & 1 deletion src/breakdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import uglify from './uglify.js'
* @function
* @category Function
* @sig String -> String -> Object
* @description Takes a provided phone string and breaks it down into an object of codes
* @description Takes a provided phone string and breaks it down into an object of codes only works loosely for NANP numbers. The gatcha here is that NANP numbers take the form of NXX NXX XXXX where N is a digit from 2-9 and X is a digit from 0-9, but in order to support placeholders we use a [_0-9]{3} check
* @param {String} phone The phone number to breakdown
* @return {Object} Returns an object of the broken down phone number
*
Expand Down
67 changes: 67 additions & 0 deletions src/breakdownWithFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import _curry2 from './_internals/_curry2.js'
import isValidWithFormat from './isValidWithFormat.js'

/**
* @name breakdownWithFormat
* @since v4.1.0
* @function
* @category Function
* @sig String -> String -> Object
* @description
* Breaks down a phone number based on a custom format provided and returns an object with the parts of the phone number
* C - Country Code A- Area Code L - Local Code N - Line Number X - Extension
* Does NOT work with placeholders
* @param {String} format The format to validate against
* @param {String} phone The phone number to breakdown
* @return {Object} Returns an object with the parts of the phone number
* @example
* import { breakdownWithFormat } from 'phone-fns'
*
* breakdownWithFormat('+C (AAA) LLL-NNNN xXXX', '+1-555-444-3333 x123') // => { countryCode: '1', areaCode: '555', localCode: '444', lineNumber: '3333', extension: '123' }
* breakdownWithFormat('AAA-LLL-NNNN', '010-XYZ-1234') // => Error: The phone number provided does not match the format provided or is an invalid phone number
*
* // it's also curried
* const fn = breakdownWithFormat('+C (AAA) LLL-NNNN xXXX')
* fn('+1-555-444-3333 x123') // => { countryCode: '', areaCode: '123', localCode: '456', lineNumber: '7890', extension: '' }
*/
function breakdownWithFormat (format, phone) {
if (!format) {
throw new Error('You must provide a format to breakdown')
}

if (!isValidWithFormat(format, phone)) {
throw new Error('The phone number provided does not match the format provided or is an invalid phone number')
}

const results = {
countryCode: '',
areaCode: '',
localCode: '',
lineNumber: '',
extension: ''
}

for (let i = 0; i < format.length; i++) {
switch (format[i]) {
case 'C':
results.countryCode += phone[i]
break
case 'A':
results.areaCode += phone[i]
break
case 'N':
results.lineNumber += phone[i]
break
case 'L':
results.localCode += phone[i]
break
case 'X':
results.extension += phone[i]
break
}
}

return results
}

export default _curry2(breakdownWithFormat)
30 changes: 30 additions & 0 deletions src/findSeparators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @name findSeparators
* @since v4.1.0
* @function
* @category Function
* @sig String -> Array
* @description
* Finds a list of separators in a phone number string
* @param {String} phone The phone number to breakdown
* @return {Array} Returns an array of separators found in the phone number
* @example
* import { findSeparators } from 'phone-fns'
*
* findSeparators('123-456-7890') // => ['-']
* findSeparators('123.456.7890') // => ['.']
* findSeparators('123 456 7890') // => [' ']
* findSeparators('1234567890') // => []
*/
function findSeparators (phone) {
const separators = ['-', '.', ' ']
const foundSeparators = []
for (const separator of separators) {
if (phone.includes(separator)) {
foundSeparators.push(separator)
}
}
return foundSeparators
}

export default findSeparators
Loading
Loading