Skip to content

Commit

Permalink
Merge 80a21c8 into 1795778
Browse files Browse the repository at this point in the history
  • Loading branch information
singuerinc committed Feb 20, 2019
2 parents 1795778 + 80a21c8 commit 5317a47
Show file tree
Hide file tree
Showing 18 changed files with 550 additions and 19 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ isOld("GI-1234-BL"); //=> true
isOld("DGP 1234 BL"); //=> true
```

### isSpecial()

Police, Air force, Army, Navy, etc. have special plates.

```js
import { isSpecial } from "spanish-car-plate";

isSpecial("DGP 3874"); //=> true
isSpecial("CNP-5764"); //=> true
isSpecial("E8720"); //=> true
```

### getCounter()

```js
Expand Down Expand Up @@ -78,6 +90,23 @@ getProvinceCode("M 1234 BL"); //=> "M"
getProvinceCode("SO-1234 BL"); //=> "SO"
```

### getSpecialCode()

```js
import { getSpecialCode } from "spanish-car-plate";

getSpecialCode("DGP1234"); //=> "DGP"
```

### getSpecialName()

```js
import { getSpecialName } from "spanish-car-plate";

getSpecialName("DGP1234"); //=> "Spanish Police"
getSpecialName("CME1234"); //=> "Corps of the Mossos d'Esquadra"
```

### parse()

Get all the information about the plate
Expand Down
135 changes: 126 additions & 9 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
6 changes: 5 additions & 1 deletion src/getNumber.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isOld } from "./isOld";
import { isValid } from "./isValid";
import { _partsNew, _partsOld } from "./_utils";
import { isSpecial } from "./isSpecial";
import { _partsNew, _partsOld, _partsSpecial } from "./_utils";

/**
*
Expand All @@ -17,6 +18,9 @@ function getNumber(value) {
if (isOld(str) === true) {
const [, num] = _partsOld(str);
return parseInt(num, 10);
} else if (isSpecial(str) === true) {
const [, num] = _partsSpecial(str);
return parseInt(num, 10);
} else if (isValid(str)) {
const [num] = _partsNew(str);
return parseInt(num, 10);
Expand Down
25 changes: 25 additions & 0 deletions src/getSpecialCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { SPECIALS } from "./specials";
import { isSpecial } from "./isSpecial";
import { _partsSpecial } from "./_utils";

/**
* Returns the special code for a valid car plate
* @param {string} value
* @returns {string}
* @since 0.0.7
* @example
* getSpecialCode("DGP-1234"); // => "DGP"
*/
function getSpecialCode(value) {
const str = !value ? "" : value;

if (!isSpecial(str)) {
return null;
}

const [code] = _partsSpecial(str);

return SPECIALS[code] ? code : null;
}

export { getSpecialCode };
19 changes: 19 additions & 0 deletions src/getSpecialName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { SPECIALS } from "./specials";
import { getSpecialCode } from "./getSpecialCode";

/**
* Returns the special name for a valid car plate
* @param {string} value
* @returns {string}
* @since 0.0.7
* @example
* getSpecialCode("CME1234"); // => "Corps of the Mossos d'Esquadra"
*/
function getSpecialName(value) {
const str = !value ? "" : value;
const code = getSpecialCode(str);

return SPECIALS[code] || null;
}

export { getSpecialName };
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export { isValid } from "./isValid";
export { isOld } from "./isOld";
export { isSpecial } from "./isSpecial";
export { getCounter } from "./getCounter";
export { getNumber } from "./getNumber";
export { getSpecialCode } from "./getSpecialCode";
export { getSpecialName } from "./getSpecialName";
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 };

0 comments on commit 5317a47

Please sign in to comment.