You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

regex.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* globals describe, expect, it */
  2. import { regex } from '../../../../src/main.js'
  3. describe('regex.js', () => {
  4. describe('numberAndUnit', () => {
  5. it('matches number and unit 12px', () => {
  6. const match = '12px'.match(regex.numberAndUnit)
  7. expect(match[1]).toBe('12')
  8. expect(match[5]).toBe('px')
  9. })
  10. it('matches number and unit 12', () => {
  11. const match = '12'.match(regex.numberAndUnit)
  12. expect(match[1]).toBe('12')
  13. expect(match[5]).toBe('')
  14. })
  15. it('matches number and unit 12%', () => {
  16. const match = '12%'.match(regex.numberAndUnit)
  17. expect(match[1]).toBe('12')
  18. expect(match[5]).toBe('%')
  19. })
  20. it('matches number and unit -12%', () => {
  21. const match = '-12%'.match(regex.numberAndUnit)
  22. expect(match[1]).toBe('-12')
  23. expect(match[5]).toBe('%')
  24. })
  25. it('matches number and unit -12.123%', () => {
  26. const match = '-12.123%'.match(regex.numberAndUnit)
  27. expect(match[1]).toBe('-12.123')
  28. expect(match[5]).toBe('%')
  29. })
  30. it('matches number and unit -12.123e12%', () => {
  31. const match = '-12.123e12%'.match(regex.numberAndUnit)
  32. expect(match[1]).toBe('-12.123e12')
  33. expect(match[5]).toBe('%')
  34. })
  35. })
  36. describe('hex', () => {
  37. it('matches a 6 digit hex', () => {
  38. const match = '#123456'.match(regex.hex)
  39. expect(match[1]).toBe('12')
  40. expect(match[2]).toBe('34')
  41. expect(match[3]).toBe('56')
  42. })
  43. /* it('does not matches without #', () => {
  44. const match = '123456'.match(regex.hex)
  45. expect(match).toBe(null)
  46. }) */
  47. it('does not matches other then 0-f #', () => {
  48. const match = '#09afhz'.match(regex.hex)
  49. expect(match).toBe(null)
  50. })
  51. it('does not matches non full hex', () => {
  52. const match = '#aaa'.match(regex.hex)
  53. expect(match).toBe(null)
  54. })
  55. })
  56. describe('rgb', () => {
  57. it('matches rgb values of rgb(...) command', () => {
  58. const match = 'rgb(12,34,56)'.match(regex.rgb)
  59. expect(match[1]).toBe('12')
  60. expect(match[2]).toBe('34')
  61. expect(match[3]).toBe('56')
  62. })
  63. it('does not match in the wrong format', () => {
  64. expect('rgb( 12 , 34 , 56)'.match(regex.rgb)).toBe(null)
  65. expect('12,34,56'.match(regex.rgb)).toBe(null)
  66. expect('(12,34,56)'.match(regex.rgb)).toBe(null)
  67. expect('rgb(aa,34,56)'.match(regex.rgb)).toBe(null)
  68. expect('rgb(12,34)'.match(regex.rgb)).toBe(null)
  69. })
  70. })
  71. describe('reference', () => {
  72. it('matches a reference', () => {
  73. const match = '#soMe_cRazy-1_id'.match(regex.reference)
  74. expect(match[1]).toBe('#soMe_cRazy-1_id')
  75. })
  76. it('tries to match malformed references', () => {
  77. const match = '#some_crazy%-1_id'.match(regex.reference)
  78. expect(match[0]).toBe('#some_crazy')
  79. })
  80. })
  81. describe('transforms', () => {
  82. it('splits a transform chain', () => {
  83. const split = 'rotate(34) translate(1,2), translate(1 , 3),rotate(12) , something(1,2,3)'.split(regex.transforms)
  84. expect(split).toEqual([ 'rotate(34', 'translate(1,2', 'translate(1 , 3', 'rotate(12', 'something(1,2,3', '' ])
  85. })
  86. })
  87. describe('whitespace', () => {
  88. it('replaces all whitespaces', () => {
  89. expect(' \n \r \t '.replace(regex.whitespace, ' ')).toBe(' ')
  90. })
  91. })
  92. describe('isHex', () => {
  93. it('returns true when testing hex values', () => {
  94. expect(regex.isHex.test('#123')).toBe(true)
  95. expect(regex.isHex.test('#abc')).toBe(true)
  96. expect(regex.isHex.test('#123456')).toBe(true)
  97. expect(regex.isHex.test('#abcdef')).toBe(true)
  98. expect(regex.isHex.test('#16fde9')).toBe(true)
  99. })
  100. it('returns false when testing non hex values', () => {
  101. expect(regex.isHex.test('#12')).toBe(false)
  102. expect(regex.isHex.test('abc')).toBe(false)
  103. expect(regex.isHex.test('#1234563')).toBe(false)
  104. expect(regex.isHex.test('#kasdhs')).toBe(false)
  105. expect(regex.isHex.test('#abcd')).toBe(false)
  106. })
  107. })
  108. describe('isRgb', () => {
  109. it('returns true when testing rgb values', () => {
  110. expect(regex.isRgb.test('rgb(1,2,3)')).toBe(true)
  111. expect(regex.isRgb.test('rgb( 3, 1,3)')).toBe(true)
  112. })
  113. it('returns false when testing non rgb values', () => {
  114. expect(regex.isRgb.test('hsl(1,2,3)')).toBe(false)
  115. expect(regex.isRgb.test('#123')).toBe(false)
  116. expect(regex.isRgb.test('something')).toBe(false)
  117. })
  118. })
  119. describe('isBlank', () => {
  120. it('returns true if something is blank', () => {
  121. expect(regex.isBlank.test('')).toBe(true)
  122. expect(regex.isBlank.test(' ')).toBe(true)
  123. expect(regex.isBlank.test('\n')).toBe(true)
  124. expect(regex.isBlank.test('\r')).toBe(true)
  125. expect(regex.isBlank.test('\t')).toBe(true)
  126. expect(regex.isBlank.test(' \n\r\t')).toBe(true)
  127. })
  128. it('returns false if something is not blank', () => {
  129. expect(regex.isBlank.test('a')).toBe(false)
  130. expect(regex.isBlank.test('1')).toBe(false)
  131. })
  132. })
  133. describe('isNumber', () => {
  134. it('returns true if something is a number', () => {
  135. expect(regex.isNumber.test('123')).toBe(true)
  136. expect(regex.isNumber.test('-123')).toBe(true)
  137. expect(regex.isNumber.test('-12.3')).toBe(true)
  138. expect(regex.isNumber.test('-12.3e12')).toBe(true)
  139. expect(regex.isNumber.test('-12.3e-12')).toBe(true)
  140. expect(regex.isNumber.test('+12.3e-12')).toBe(true)
  141. expect(regex.isNumber.test('+12.3E-12')).toBe(true)
  142. })
  143. it('returns false if something is not a number', () => {
  144. expect(regex.isNumber.test('a')).toBe(false)
  145. expect(regex.isNumber.test('-a')).toBe(false)
  146. expect(regex.isNumber.test('-12a')).toBe(false)
  147. expect(regex.isNumber.test('-12.3a12')).toBe(false)
  148. expect(regex.isNumber.test('-12.3e-1a')).toBe(false)
  149. expect(regex.isNumber.test('12.12.12')).toBe(false)
  150. expect(regex.isNumber.test('12.12e12.3')).toBe(false)
  151. expect(regex.isNumber.test('12.12e12e4')).toBe(false)
  152. })
  153. })
  154. describe('isImage', () => {
  155. it('returns true if something is an image filename', () => {
  156. expect(regex.isImage.test('a.jpg')).toBe(true)
  157. expect(regex.isImage.test('a.jpeg')).toBe(true)
  158. expect(regex.isImage.test('a.png')).toBe(true)
  159. expect(regex.isImage.test('a.gif')).toBe(true)
  160. expect(regex.isImage.test('a.svg')).toBe(true)
  161. })
  162. it('returns false if something is not an image filename', () => {
  163. expect(regex.isImage.test('a.abc')).toBe(false)
  164. expect(regex.isImage.test('a.txt')).toBe(false)
  165. expect(regex.isImage.test('a.doc')).toBe(false)
  166. })
  167. })
  168. describe('delimiter', () => {
  169. it('splits at whitespace and comma', () => {
  170. const split = '1,2 3 , 4 5,, 6'.split(regex.delimiter)
  171. expect(split).toEqual([ '1', '2', '3', '4', '5', '6' ])
  172. })
  173. })
  174. describe('isPathLetter', () => {
  175. it('returns true if something is a path letter', () => {
  176. 'MLHVCSQTAZmlhvcsqtaz'.split('').forEach((l) => {
  177. expect(regex.isPathLetter.test(l)).toBe(true)
  178. })
  179. })
  180. it('returns false if something is not path letter', () => {
  181. '123biuBIU$%&'.split('').forEach((l) => {
  182. expect(regex.isPathLetter.test(l)).toBe(false)
  183. })
  184. })
  185. })
  186. })