| Lines of Code | 57 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | import {dateUnslug} from '../../' |
||
| 2 | |||
| 3 | var fullAttr = '-abe-(.+?)(?=\.' |
||
| 4 | var captureAttr = '-abe-(.+?)(?=\.' |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Class Attr |
||
| 8 | * Work string to manage string attributes key/value |
||
| 9 | */ |
||
| 10 | export default class Attr { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @param {String} str string to work with |
||
| 14 | * @return {void} |
||
| 15 | */ |
||
| 16 | constructor(str) { |
||
| 17 | this.str = str |
||
| 18 | this.val = {} |
||
| 19 | this.extract() |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return {Object} attributes extracted from string as an object |
||
| 24 | */ |
||
| 25 | extract() { |
||
| 26 | var rex = new RegExp(captureAttr + this.getExtension() + ')') |
||
| 27 | if(rex.test(this.str)) { |
||
| 28 | var arrAttr = this.str.match(rex)[0].replace('-abe-', '') |
||
| 29 | this.val = {'s': arrAttr[0], 'd': dateUnslug(arrAttr.slice(1), this.str)} |
||
| 30 | } |
||
| 31 | return this.val |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return {String} str without attributes |
||
| 36 | */ |
||
| 37 | remove() { |
||
| 38 | return this.str.replace(new RegExp(fullAttr + this.getExtension() + ')'), '') |
||
| 39 | } |
||
| 40 | |||
| 41 | getExtension(){ |
||
| 42 | var ext = this.str.split('.') |
||
| 43 | return ext[ext.length - 1] |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Insert attributes to the string |
||
| 48 | * @param {String} string composed of a status (ex: v for validate, d for draft ...) and a date |
||
|
|
|||
| 49 | * @return {String} the new string with added attributes |
||
| 50 | */ |
||
| 51 | insert(newValues) { |
||
| 52 | var strWithoutAttr = this.remove() |
||
| 53 | strWithoutAttr = strWithoutAttr.replace(new RegExp('\\.' + this.getExtension()), '') |
||
| 54 | return strWithoutAttr + '-abe-' + newValues + '.' + this.getExtension() |
||
| 55 | } |
||
| 56 | |||
| 57 | } |