-
Notifications
You must be signed in to change notification settings - Fork 0
Port format-location to Typescript #4
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
Open
CamillaTeodoro
wants to merge
21
commits into
master
Choose a base branch
from
SLK-15-Port-format-location-to-Typescript
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
42c3e3c
chore: migrate from npm to yarn
CamillaTeodoro 0eff765
chore: update travis to run node version 14
CamillaTeodoro de572b0
chore: install babel 7
CamillaTeodoro ddf0aba
chore: install goodeggs toolkit
CamillaTeodoro 95807ec
chore: add configuration to Typescript
CamillaTeodoro f3d44d1
chore: decaffeinate
CamillaTeodoro 0fd0d9e
chore: Convert index.js and test.js to TS
CamillaTeodoro 2c2e25a
chore: change npm to yarn in README
CamillaTeodoro 27f8439
chore: update yarn.lock
CamillaTeodoro 83057a9
chore: add script in .travis.yml
CamillaTeodoro 4ff0f3d
chore: add .npmrc file
CamillaTeodoro 28238c4
chore: update yarn.lock
CamillaTeodoro b4fe961
chore: change scripts
CamillaTeodoro 1457bf8
chore: fix lint error
CamillaTeodoro ba514a4
chore: upgrade goodeggs/toolkit to v8
CamillaTeodoro 6d4ba33
Update to node 16
CamillaTeodoro d3ea89c
fix: remove unnecessary files
CamillaTeodoro 0843570
refactor: use nullish coalescing operator
CamillaTeodoro 4557110
fix: remove unnecessary return statement
CamillaTeodoro 01fde4c
chore: use node 18
CamillaTeodoro aa88146
chore: update mocha
CamillaTeodoro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Temporary entrypoint wrapper for various `yarn` scripts to configure @babel/register for TypeScript | ||
| // TODO(murkey) Remove this file and: | ||
| // - Use this wrapper: https://github.com/deepsweet/babel-register-ts | ||
| // - Get a new version of @babel/register with built-in support: https://github.com/babel/babel/pull/6027 | ||
| // - Get a version of babel that allows extensions to be configured via .babelrc/package.json: https://github.com/babel/babel/issues/3741 | ||
| require('@babel/register')({ | ||
| extensions: ['.ts', '.js'], | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| node_modules | ||
| lib | ||
| .eslintcache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module.exports = { | ||
| root: true, | ||
| parserOptions: { | ||
| project: ['./tsconfig.json'], | ||
| }, | ||
| extends: ['plugin:goodeggs/recommended', 'plugin:goodeggs/typescript'], | ||
| env: { | ||
| node: true, | ||
| }, | ||
| rules: {'import/no-commonjs': 'off'}, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| node_modules/ | ||
| npm-debug.log | ||
| .DS_Store | ||
| yarn-error.log | ||
| .eslintcache | ||
| /lib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 18.12.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| registry = https://registry.npmjs.org/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require("@goodeggs/toolkit/config/prettier"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| language: node_js | ||
| node_js: | ||
| - '0.10' | ||
| - '16' | ||
| script: yarn test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,26 @@ | ||
| # Format Location | ||
| [](http://badge.fury.io/js/format-location) | ||
|
|
||
|
|
||
| [](http://badge.fury.io/js/format-location) | ||
|
|
||
| [](https://travis-ci.org/goodeggs/format-location) | ||
|
|
||
|
|
||
| Format location objects in a number of standard ways | ||
|
|
||
| ## Contributing | ||
|
|
||
| ``` | ||
| $ git clone https://github.com/goodeggs/format-location && cd format-location | ||
| $ npm install | ||
| $ npm test | ||
| $ yarn install | ||
| $ yarn test | ||
| ``` | ||
|
|
||
| ## Code of Conduct | ||
|
|
||
| [Code of Conduct](https://github.com/goodeggs/format-location/blob/master/CODE_OF_CONDUCT.md) | ||
| for contributing to or participating in this project. | ||
|
|
||
| ## License | ||
|
|
||
| [MIT](https://github.com/goodeggs/format-location/blob/master/LICENSE.md) | ||
|
|
||
|
|
||
|
|
||
| _Module scaffold generated by [generator-goodeggs-npm](https://github.com/goodeggs/generator-goodeggs-npm)._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module.exports = { | ||
| presets: [ | ||
| [ | ||
| '@babel/preset-env', | ||
| { | ||
| targets: { | ||
| node: 14, | ||
| }, | ||
| }, | ||
| ], | ||
| '@babel/typescript', | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,46 @@ | ||
| // Generated by CoffeeScript 1.9.3 | ||
| var _locationFormats, containsText, formatLocation, formatObject; | ||
| "use strict"; | ||
|
|
||
| _locationFormats = { | ||
| full: ['%(addressLine), %(city), %(state) %(zip)', ', '], | ||
| fullGeocoded: ['%(addressLine), %(city), %(state) %(zip)@%(lat),%(lng)', ', '], | ||
| fullMultiline: ['%(addressLine)\n%(city), %(state) %(zip)', '\n'], | ||
| city: ['%(addressLine), %(city)', ', '], | ||
| zip: ['%(addressLine), %(zip)', ', '], | ||
| cityStateZip: ['%(city), %(state) %(zip)', ''], | ||
| short: ['%(addressLine)', ', '], | ||
| shortMultiline: ['%(addressLine)', '\n'], | ||
| shortVague: ['%(vagueAddress)', ', '] | ||
| }; | ||
|
|
||
| formatObject = function(obj, formatString) { | ||
| var paramRegex; | ||
| paramRegex = /%\((\w+)\)/g; | ||
| return formatString.replace(paramRegex, function(match, key) { | ||
| return obj[key] || ''; | ||
| }); | ||
| }; | ||
|
|
||
| containsText = function(string) { | ||
| return !!(string != null ? string.trim().length : void 0); | ||
| }; | ||
|
|
||
| module.exports = formatLocation = function(location, formatString) { | ||
| var addressLineSeparator, coords, format, ref, ref1, ref2, ref3; | ||
| if (!(location.city && location.zip && location.state)) { | ||
| return location.address; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.default = void 0; | ||
| class FormatLocation { | ||
| _locationFormats = { | ||
| full: ['%(addressLine), %(city), %(state) %(zip)', ', '], | ||
| fullGeocoded: ['%(addressLine), %(city), %(state) %(zip)@%(lat),%(lng)', ', '], | ||
| fullMultiline: ['%(addressLine)\n%(city), %(state) %(zip)', '\n'], | ||
| city: ['%(addressLine), %(city)', ', '], | ||
| zip: ['%(addressLine), %(zip)', ', '], | ||
| cityStateZip: ['%(city), %(state) %(zip)', ''], | ||
| short: ['%(addressLine)', ', '], | ||
| shortMultiline: ['%(addressLine)', '\n'], | ||
| shortVague: ['%(vagueAddress)', ', '] | ||
| }; | ||
| formatObject(obj, formatString) { | ||
| const paramRegex = /%\((\w+)\)/g; | ||
| return formatString.replace(paramRegex, (_match, key) => { | ||
| const value = obj[key]; | ||
| return typeof value === 'object' ? JSON.stringify(value) : value || ''; | ||
| }); | ||
| } | ||
| ref = _locationFormats[formatString] || [formatString, ', '], format = ref[0], addressLineSeparator = ref[1]; | ||
| location = JSON.parse(JSON.stringify(location)); | ||
| location.addressLine = [location.address, location.address2].filter(containsText).join(addressLineSeparator); | ||
| location.vagueAddress = location.vagueAddress || location.addressLine; | ||
| coords = (ref1 = location.coordinates) != null ? ref1 : location.coords; | ||
| location.lat = (ref2 = coords != null ? coords.latitude : void 0) != null ? ref2 : coords != null ? coords.lat : void 0; | ||
| location.lng = (ref3 = coords != null ? coords.longitude : void 0) != null ? ref3 : coords != null ? coords.lng : void 0; | ||
| return formatObject(location, format); | ||
| }; | ||
|
|
||
| formatLocation.formats = _locationFormats; | ||
| containsText(string) { | ||
| return string != null ? string.trim().length : undefined; | ||
| } | ||
| formatLocation(location, formatString) { | ||
| // if there isn't `city`, `zip` and `state`, assume this location object hasn't been converted | ||
| // to the full details structure and the entire address is in the `address` field | ||
| if (!location.city || !location.zip || !location.state || formatString === undefined) { | ||
| return location.address || ''; | ||
| } | ||
| const [format, addressLineSeparator] = this._locationFormats[formatString] !== undefined ? this._locationFormats[formatString] : [formatString, ', ']; | ||
| location = JSON.parse(JSON.stringify(location)); | ||
| location.addressLine = [location.address, location.address2].filter(this.containsText).join(addressLineSeparator); | ||
| location.vagueAddress = location.vagueAddress || location.addressLine; | ||
| const coords = location.coordinates ?? location.coords; | ||
| location.lat = (coords === null || coords === void 0 ? void 0 : coords.latitude) ?? (coords === null || coords === void 0 ? void 0 : coords.lat) ?? undefined; | ||
| location.lng = (coords === null || coords === void 0 ? void 0 : coords.longitude) ?? (coords === null || coords === void 0 ? void 0 : coords.lng) ?? undefined; | ||
| return this.formatObject(location, format); | ||
| } | ||
| } | ||
| var _default = FormatLocation; | ||
| exports.default = _default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,15 +15,31 @@ | |
| "homepage": "https://github.com/goodeggs/format-location", | ||
| "bugs": "https://github.com/goodeggs/format-location/issues", | ||
| "devDependencies": { | ||
| "coffee-script": ">=1.7.x", | ||
| "mocha": "^1.x.x", | ||
| "chai": "^1.9.1" | ||
| "@babel/cli": "^7.22.5", | ||
| "@babel/core": "^7.22.5", | ||
| "@babel/polyfill": "^7.12.1", | ||
| "@babel/preset-env": "^7.22.5", | ||
| "@babel/preset-typescript": "^7.22.5", | ||
| "@babel/register": "^7.22.5", | ||
|
Comment on lines
+18
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uff, we could use tsc to build this lib instead of depending on 6 libs only to run babel... something for the future! |
||
| "@goodeggs/toolkit": "^8.0.0", | ||
| "@goodeggs/tsconfig": "^2.0.2", | ||
| "@types/chai": "^4.3.5", | ||
| "@types/mocha": "^10.0.1", | ||
| "chai": "^4.3.7", | ||
| "mocha": "^10.2.0", | ||
| "typescript": "^5.1.3" | ||
| }, | ||
| "scripts": { | ||
| "compile": "coffee --bare --compile --output lib/ src/", | ||
| "build": "npm run compile", | ||
| "test": "mocha", | ||
| "pretest": "npm run build" | ||
| "build": "yarn run build:clean && babel --extensions=.ts,.js,.jsx,.tsx src -d lib && yarn run build:types", | ||
| "build:clean": "rm -rf lib", | ||
| "build:types": "tsc --project tsconfig.declarations.json", | ||
| "prepublishOnly": "yarn run build", | ||
| "lint": "getk run lint-es '**/*.{js,jsx,ts,tsx}' --ignore-path .gitignore", | ||
| "lint:fix": "getk run fix-es --ignore-path .gitignore", | ||
| "test": " yarn run typecheck && yarn run test:mocha && yarn run lint ", | ||
| "test:mocha": "yarn run test:mocha:glob 'src/**/*test.ts'", | ||
| "test:mocha:glob": "NODE_ENV=test mocha --require @babel/polyfill --require .babel.register.ts --extension ts,js,jsx,tsx", | ||
| "typecheck": "tsc" | ||
| }, | ||
| "publishConfig": { | ||
| "registry": "https://registry.npmjs.org/", | ||
|
|
||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import {describe, it} from 'mocha'; | ||
| import {expect} from 'chai'; | ||
|
|
||
| import FormatLocation, {Location} from '.'; | ||
|
|
||
| describe('formatLocation', function () { | ||
| let location: Location = {}; | ||
|
|
||
| beforeEach( | ||
| () => | ||
| (location = { | ||
| name: 'Good Eggs HQ', | ||
| address: '530 Hampshire Street', | ||
| address2: 'Suite 301', | ||
| city: 'San Francisco', | ||
| state: 'CA', | ||
| zip: '94110', | ||
| coordinates: { | ||
| latitude: '37.7627904', | ||
| longitude: '-122.4084761', | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| it('formats the full address', () => | ||
| expect(new FormatLocation().formatLocation(location, 'full')).to.equal( | ||
| '530 Hampshire Street, Suite 301, San Francisco, CA 94110', | ||
| )); | ||
|
|
||
| it('formats the full geocoded address', () => | ||
| expect(new FormatLocation().formatLocation(location, 'fullGeocoded')).to.equal( | ||
| '530 Hampshire Street, Suite 301, San Francisco, CA 94110@37.7627904,-122.4084761', | ||
| )); | ||
|
|
||
| it('formats the full multiline address', () => | ||
| expect(new FormatLocation().formatLocation(location, 'fullMultiline')).to.equal( | ||
| '530 Hampshire Street\nSuite 301\nSan Francisco, CA 94110', | ||
| )); | ||
|
|
||
| it('formats the address with city', () => | ||
| expect(new FormatLocation().formatLocation(location, 'city')).to.equal( | ||
| '530 Hampshire Street, Suite 301, San Francisco', | ||
| )); | ||
|
|
||
| it('formats short address', () => | ||
| expect(new FormatLocation().formatLocation(location, 'short')).to.equal( | ||
| '530 Hampshire Street, Suite 301', | ||
| )); | ||
|
|
||
| it('formats the short multiline address', () => | ||
| expect(new FormatLocation().formatLocation(location, 'shortMultiline')).to.equal( | ||
| '530 Hampshire Street\nSuite 301', | ||
| )); | ||
|
|
||
| it('formats custom address format strings', () => | ||
| expect(new FormatLocation().formatLocation(location, '%(zip); %(city); %(name)')).to.equal( | ||
| '94110; San Francisco; Good Eggs HQ', | ||
| )); | ||
|
|
||
| it('formats location with just the address field', () => | ||
| expect(new FormatLocation().formatLocation({address: 'hello world'})).to.equal('hello world')); | ||
|
|
||
| it('shortVague returns the short address', () => | ||
| expect(new FormatLocation().formatLocation(location, 'shortVague')).to.equal( | ||
| '530 Hampshire Street, Suite 301', | ||
| )); | ||
|
|
||
| describe('a vague address in the location', function () { | ||
| beforeEach(() => (location.vagueAddress = 'Hampshire & Mariposa (by the slow club)')); | ||
|
|
||
| it('shortVague returns vague address', () => | ||
| expect(new FormatLocation().formatLocation(location, 'shortVague')).to.equal( | ||
| location.vagueAddress, | ||
| )); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another reason to use only tsc? 🤷🏻