Skip to content

Commit

Permalink
Add pretter dev dependency; Apply new standards
Browse files Browse the repository at this point in the history
  • Loading branch information
cblanc committed Mar 14, 2019
1 parent b2eefe1 commit a287486
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
46 changes: 23 additions & 23 deletions lib/index.ts
Expand Up @@ -5,44 +5,44 @@ const containsAmpersand = /\w+&\w+/;

// capitalise word with exceptions on exclusion list
const capitaliseWord = (word: string): string => {
word = word.toLowerCase();
if (word.match(exclusion)) return word;
if (word.match(containsAmpersand)) return word.toUpperCase();
word = word.toLowerCase();
if (word.match(exclusion)) return word;
if (word.match(containsAmpersand)) return word.toUpperCase();

return word.charAt(0).toUpperCase() + word.slice(1);
return word.charAt(0).toUpperCase() + word.slice(1);
};

const joiner = /-/;
const joinerWord = /^(in|de|under|upon|y|on|over|the|by)$/;

// Check for names connected with hyphens
const checkJoins = (string: string): string => {
if (string.match(joiner) === null) return string;
if (string.match(joiner) === null) return string;

return string
.split("-")
.map(str => {
if (str.match(joinerWord)) return str.toLowerCase();
return string
.split("-")
.map(str => {
if (str.match(joinerWord)) return str.toLowerCase();

return capitaliseWord(str);
})
.join("-")
}
return capitaliseWord(str);
})
.join("-");
};

const boness = /bo'ness/i;

// Handles unusual names which cannot be easily generalised into a rule
const exceptions = (str: string): string => {
if (str.match(boness)) return "Bo'Ness";
if (str.match(boness)) return "Bo'Ness";

return str;
}
return str;
};

export const capitalisePostTown = (postTown: string): string => {
return postTown
.split(" ")
.map(capitaliseWord)
.map(checkJoins)
.map(exceptions)
.join(" ");
}
return postTown
.split(" ")
.map(capitaliseWord)
.map(checkJoins)
.map(exceptions)
.join(" ");
};
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -75,6 +75,7 @@
"coveralls": "^3.0.1",
"mocha": "~6.0.0",
"nyc": "~13.3.0",
"prettier": "~1.16.4",
"source-map-support": "~0.5.6",
"ts-node": "~8.0.2",
"tslint": "~5.14.0",
Expand Down
22 changes: 12 additions & 10 deletions test/index.ts
@@ -1,20 +1,22 @@
"use strict";

import * as fs from "fs";
import * as path from "path";
import { readFileSync } from "fs";
import { join } from "path";
import { assert } from "chai";
import { capitalisePostTown } from "../lib/index";

const posttowns = fs.readFileSync(path.join(__dirname, "./post_towns.csv"), { encoding: "utf8" })
.split("\n")
.map(posttown => posttown.trim());
const posttowns = readFileSync(join(__dirname, "./post_towns.csv"), {
encoding: "utf8"
})
.split("\n")
.map(posttown => posttown.trim());

const capitalisedPosttowns = posttowns.map(posttown => posttown.toUpperCase());

describe("capitalise", () => {
for (let i = 0; i < posttowns.length; i++) {
it (`correctly capitalises ${posttowns[i]}`, () => {
assert.equal(capitalisePostTown(capitalisedPosttowns[i]), posttowns[i]);
});
}
for (let i = 0; i < posttowns.length; i++) {
it(`correctly capitalises ${posttowns[i]}`, () => {
assert.equal(capitalisePostTown(capitalisedPosttowns[i]), posttowns[i]);
});
}
});
6 changes: 2 additions & 4 deletions tsconfig.json
@@ -1,9 +1,7 @@
{
"extends": "@cablanchard/tsconfig",
"compilerOptions": {
"lib": []
"outDir": "dist"
},
"include": [
"lib/**/*"
]
"include": ["lib/**/*"]
}

0 comments on commit a287486

Please sign in to comment.