Completed
Push — master ( 1741bf...ba793d )
by
unknown
02:02
created

attr.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 1
c 5
b 0
f 0
nc 1
dl 0
loc 5
rs 9.4285
nop 1
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
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
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
}