No Description

animation-options-parser.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Copyright 2013-2015 ASIAL CORPORATION
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. 'use strict';
  14. var startsWith = function startsWith(s, c) {
  15. return s.substr(0, c.length) === c;
  16. };
  17. var endsWith = function endsWith(s, c) {
  18. return s.substr(s.length - c.length, c.length) === c;
  19. };
  20. var unwrap = function unwrap(s) {
  21. return s.slice(1, -1);
  22. };
  23. var isObjectString = function isObjectString(s) {
  24. return startsWith(s, '{') && endsWith(s, '}');
  25. };
  26. var isArrayString = function isArrayString(s) {
  27. return startsWith(s, '[') && endsWith(s, ']');
  28. };
  29. var isQuotedString = function isQuotedString(s) {
  30. return startsWith(s, '\'') && endsWith(s, '\'') || startsWith(s, '"') && endsWith(s, '"');
  31. };
  32. var error = function error(token, string, originalString) {
  33. throw new Error('Unexpected token \'' + token + '\' at position ' + (originalString.length - string.length - 1) + ' in string: \'' + originalString + '\'');
  34. };
  35. var processToken = function processToken(token, string, originalString) {
  36. if (token === 'true' || token === 'false') {
  37. return token === 'true';
  38. } else if (isQuotedString(token)) {
  39. return unwrap(token);
  40. } else if (!isNaN(token)) {
  41. return +token;
  42. } else if (isObjectString(token)) {
  43. return parseObject(unwrap(token));
  44. } else if (isArrayString(token)) {
  45. return parseArray(unwrap(token));
  46. } else {
  47. error(token, string, originalString);
  48. }
  49. };
  50. var nextToken = function nextToken(string) {
  51. string = string.trim();
  52. var limit = string.length;
  53. if (string[0] === ':' || string[0] === ',') {
  54. limit = 1;
  55. } else if (string[0] === '{' || string[0] === '[') {
  56. var c = string.charCodeAt(0);
  57. var nestedObject = 1;
  58. for (var i = 1; i < string.length; i++) {
  59. if (string.charCodeAt(i) === c) {
  60. nestedObject++;
  61. } else if (string.charCodeAt(i) === c + 2) {
  62. nestedObject--;
  63. if (nestedObject === 0) {
  64. limit = i + 1;
  65. break;
  66. }
  67. }
  68. }
  69. } else if (string[0] === '\'' || string[0] === '"') {
  70. for (var _i = 1; _i < string.length; _i++) {
  71. if (string[_i] === string[0]) {
  72. limit = _i + 1;
  73. break;
  74. }
  75. }
  76. } else {
  77. for (var _i2 = 1; _i2 < string.length; _i2++) {
  78. if ([' ', ',', ':'].indexOf(string[_i2]) !== -1) {
  79. limit = _i2;
  80. break;
  81. }
  82. }
  83. }
  84. return string.slice(0, limit);
  85. };
  86. var parseObject = function parseObject(string) {
  87. var isValidKey = function isValidKey(key) {
  88. return (/^[A-Z_$][A-Z0-9_$]*$/i.test(key)
  89. );
  90. };
  91. string = string.trim();
  92. var originalString = string;
  93. var object = {};
  94. var readingKey = true,
  95. key = void 0,
  96. previousToken = void 0,
  97. token = void 0;
  98. while (string.length > 0) {
  99. previousToken = token;
  100. token = nextToken(string);
  101. string = string.slice(token.length, string.length).trim();
  102. if (token === ':' && (!readingKey || !previousToken || previousToken === ',') || token === ',' && readingKey || token !== ':' && token !== ',' && previousToken && previousToken !== ',' && previousToken !== ':') {
  103. error(token, string, originalString);
  104. } else if (token === ':' && readingKey && previousToken) {
  105. previousToken = isQuotedString(previousToken) ? unwrap(previousToken) : previousToken;
  106. if (isValidKey(previousToken)) {
  107. key = previousToken;
  108. readingKey = false;
  109. } else {
  110. throw new Error('Invalid key token \'' + previousToken + '\' at position 0 in string: \'' + originalString + '\'');
  111. }
  112. } else if (token === ',' && !readingKey && previousToken) {
  113. object[key] = processToken(previousToken, string, originalString);
  114. readingKey = true;
  115. }
  116. }
  117. if (token) {
  118. object[key] = processToken(token, string, originalString);
  119. }
  120. return object;
  121. };
  122. var parseArray = function parseArray(string) {
  123. string = string.trim();
  124. var originalString = string;
  125. var array = [];
  126. var previousToken = void 0,
  127. token = void 0;
  128. while (string.length > 0) {
  129. previousToken = token;
  130. token = nextToken(string);
  131. string = string.slice(token.length, string.length).trim();
  132. if (token === ',' && (!previousToken || previousToken === ',')) {
  133. error(token, string, originalString);
  134. } else if (token === ',') {
  135. array.push(processToken(previousToken, string, originalString));
  136. }
  137. }
  138. if (token) {
  139. if (token !== ',') {
  140. array.push(processToken(token, string, originalString));
  141. } else {
  142. error(token, string, originalString);
  143. }
  144. }
  145. return array;
  146. };
  147. var parse = function parse(string) {
  148. string = string.trim();
  149. if (isObjectString(string)) {
  150. return parseObject(unwrap(string));
  151. } else if (isArrayString(string)) {
  152. return parseArray(unwrap(string));
  153. } else {
  154. throw new Error('Provided string must be object or array like: ' + string);
  155. }
  156. };
  157. export default parse;