Skip to content

Commit

Permalink
feat: isSpecial
Browse files Browse the repository at this point in the history
  • Loading branch information
singuerinc committed Feb 19, 2019
1 parent 1795778 commit 54d9276
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 3 deletions.
56 changes: 56 additions & 0 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spanish-car-plate",
"version": "0.0.6",
"version": "0.0.7",
"description": "Spanish Car Plate validation",
"main": "dist/index.js",
"license": "MIT",
Expand Down
9 changes: 9 additions & 0 deletions src/_utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export function _partsSpecial(str) {
const cleaned = str.replace(
/^[\s]*([CMEDGPNATFSHMORW]{1,5})[^A-Z0-9]*([0-9]{4})[\s]*$/i,
"$1,$2"
);

return cleaned.split(",");
}

export function _partsOld(str) {
const cleaned = str.replace(
/^[\s]*([A-Z]{1,3})[^A-Z0-9]*([0-9]{4})[^A-Z0-9]*([A-Z]{2})[\s]*$/i,
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export { isValid } from "./isValid";
export { isOld } from "./isOld";
export { isSpecial } from "./isSpecial";
export { getCounter } from "./getCounter";
export { getNumber } from "./getNumber";
export { getProvinceName } from "./getProvinceName";
export { getProvinceCode } from "./getProvinceCode";
export { parse } from "./parse";
export { PROVINCES } from "./provinces";
export { SPECIALS } from "./specials";
29 changes: 29 additions & 0 deletions src/isSpecial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { _partsSpecial } from "./_utils";
import { SPECIALS } from "./specials";

/**
* Returns true if is a valid spacial car plate
* @param {string} value
* @returns {boolean}
* @since 0.0.7
* @example
* isSpecial("CME 2342"); // => true
* isSpecial("E 1660"); // => true
*/
function isSpecial(value) {
const str = !value ? "" : value;
const [code, num] = _partsSpecial(str);
const cleaned = `${code}${num}`;

if (cleaned.length < 5 || cleaned.length > 9) {
return false;
}

if (!SPECIALS[code]) {
return false;
}

return /^[CMEDGPNATFSHMORW]{1,5}[0-9]{4}$/i.test(cleaned);
}

export { isSpecial };
18 changes: 18 additions & 0 deletions src/specials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const SPECIALS = {
CME: "Corps of the Mossos d'Esquadra",
DGP: "Spanish Police",
CNP: "Spanish Police",
E: "Autonomous police force of the Basque Country",
EA: "Air Force",
ET: "Spanish Army",
FAE: "Allied Forces in Spain",
FN: "Spanish Navy",
GSH: "Colonial police on Sahara",
PGC: "Spanish civil guard",
MF: "Public Works Ministry",
MMA: "Environment Ministry",
MOP: "Public Works Ministry",
PME: "State owned vehicles",
PMM: "State owned vehicles, on a Ministry",
Crown: "King's Car"
};
5 changes: 5 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
isValid,
parse,
isOld,
isSpecial,
getCounter,
getProvinceName,
getProvinceCode,
Expand All @@ -22,6 +23,10 @@ describe("api", () => {
assert.ok(isOld);
});

it("should export isSpecial", () => {
assert.ok(isSpecial);
});

it("should export getProvinceName", () => {
assert.ok(getProvinceName);
});
Expand Down
88 changes: 88 additions & 0 deletions test/isSpecial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const assert = require("assert");
const { isSpecial } = require("../dist/index");

describe("#isSpecial", () => {
describe("invalid", () => {
it("should not be valid without all chars required", () => {
assert.equal(isSpecial("X"), false);
});

it("should not be valid with null", () => {
assert.equal(isSpecial(null), false);
});

it("should not be valid with undefined", () => {
assert.equal(isSpecial(undefined), false);
});

it("should not be valid with empty string", () => {
assert.equal(isSpecial(""), false);
});
});

describe("valid", () => {
it("should be valid with Crown", () => {
assert.equal(isSpecial("Crown 0000"), true);
});

it("should be valid with CME", () => {
assert.equal(isSpecial("CME 0001"), true);
});

it("should be valid with DGP", () => {
assert.equal(isSpecial("DGP 0001"), true);
});

it("should be valid with CNP", () => {
assert.equal(isSpecial("CNP 0001"), true);
});

it("should be valid with E", () => {
assert.equal(isSpecial("E 0001"), true);
});

it("should be valid with EA", () => {
assert.equal(isSpecial("EA 0001"), true);
});

it("should be valid with ET", () => {
assert.equal(isSpecial("ET 0001"), true);
});

it("should be valid with FAE", () => {
assert.equal(isSpecial("FAE 0001"), true);
});

it("should be valid with FN", () => {
assert.equal(isSpecial("FN 0001"), true);
});

it("should be valid with GSH", () => {
assert.equal(isSpecial("GSH 0001"), true);
});

it("should be valid with PGC", () => {
assert.equal(isSpecial("PGC 0001"), true);
});

it("should be valid with MF", () => {
assert.equal(isSpecial("MF 0001"), true);
});

it("should be valid with MMA", () => {
assert.equal(isSpecial("MMA 0001"), true);
});

it("should be valid with MOP", () => {
assert.equal(isSpecial("MOP 0001"), true);
});

it("should be valid with PME", () => {
assert.equal(isSpecial("PME 0001"), true);
});

it("should be valid with PMM", () => {
assert.equal(isSpecial("PMM 0001"), true);
});
});
});
6 changes: 5 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare function getNumber(value: string): number;
declare function getProvinceCode(value: string): string;
declare function getProvinceName(value: string): string;

declare function isSpecial(value: string): boolean;
/**
* Returns true if is a valid (old system 1971-2000) car plate
*/
Expand All @@ -24,14 +25,17 @@ interface IPlate {
declare function parse(value: string): IPlate;

declare const PROVINCES: object;
declare const SPECIALS: object;

export {
IPlate,
isValid,
isOld,
isSpecial,
getCounter,
getProvinceName,
getProvinceCode,
getNumber,
PROVINCES
PROVINCES,
SPECIALS
};

0 comments on commit 54d9276

Please sign in to comment.