Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions .babel.register.ts
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
Comment on lines +1 to +5
Copy link
Copy Markdown

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? 🤷🏻

require('@babel/register')({
extensions: ['.ts', '.js'],
});
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
lib
.eslintcache
11 changes: 11 additions & 0 deletions .eslintrc.js
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'},
};
3 changes: 3 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.12.0
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry = https://registry.npmjs.org/
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@goodeggs/toolkit/config/prettier");
3 changes: 2 additions & 1 deletion .travis.yml
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
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
# Format Location
[![NPM version](https://badge.fury.io/js/format-location.png)](http://badge.fury.io/js/format-location)


[![NPM version](https://badge.fury.io/js/format-location.png)](http://badge.fury.io/js/format-location)

[![Build Status](https://travis-ci.org/goodeggs/format-location.png)](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)._
13 changes: 13 additions & 0 deletions babel.config.js
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',
],
};
85 changes: 44 additions & 41 deletions lib/index.js
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;
30 changes: 23 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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/",
Expand Down
36 changes: 0 additions & 36 deletions src/index.coffee

This file was deleted.

76 changes: 76 additions & 0 deletions src/index.test.ts
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,
));
});
});
Loading