Completed
Push — master ( 976119...75ac8d )
by greg
17s
created

src/cli/cms/data/attr.js (1 issue)

1
import {cmsData} from '../../'
2
3
const captureAttr = '-abe-([a-z A-Z]{1}[0-9]{1}.+?)(?=.'
4
/**
5
 * Class Attr
6
 * Work string to manage string attributes key/value
7
 */
8
export default class Attr {
9
  /**
10
   * @param  {String} str string to work with
11
   * @return {void}
12
   */
13
  constructor(str) {
14
    this.str = str
15
    this.val = {}
16
    this.extract()
17
  }
18
19
  /**
20
   * @return {Object} attributes extracted from string as an object
21
   */
22
  extract() {
23
    var rex = new RegExp(captureAttr + this.getExtension() + ')')
24
    if (rex.test(this.str)) {
25
      var arrAttr = this.str.match(rex)[0].replace('-abe-', '')
26
      this.val = {
27
        s: arrAttr[0],
28
        d: cmsData.revision.getStatusAndDateToFileName(arrAttr.slice(1))
29
      }
30
    }
31
    return this.val
32
  }
33
34
  /**
35
   * @return {String} str without attributes
36
   */
37
  remove() {
38
    if (this.str != null) {
39
      return this.str.replace(
40
        new RegExp(captureAttr + this.getExtension() + ')'),
41
        ''
42
      )
43
    }
44
    return this.str
45
  }
46
47
  getExtension() {
48
    if (this.str != null) {
49
      var ext = this.str.split('.')
50
      return ext[ext.length - 1]
51
    }
52
    return ''
53
  }
54
55
  /**
56
   * Insert attributes to the string
57
   * @param  {String} string composed of a status (ex: v for validate, d for draft ...) and a date
0 ignored issues
show
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
58
   * @return {String} the new string with added attributes
59
   */
60
  insert(newValues) {
61
    var strWithoutAttr = this.remove()
62
    strWithoutAttr = strWithoutAttr.replace(
63
      new RegExp('\\.' + this.getExtension()),
64
      ''
65
    )
66
    return strWithoutAttr + '-abe-' + newValues + '.' + this.getExtension()
67
  }
68
}
69