a409a3499d911a0f449d677dc7027f56ba1d7b6b61ac7c7878acabd24d3b772d78d823e17169df2743753b0e4988b093c1bc0670724f2bf5307282e9c75529 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import {List} from 'immutable';
  2. import {Value} from './index';
  3. /**
  4. * Sass's [number type](https://sass-lang.com/documentation/values/numbers).
  5. *
  6. * @category Custom Function
  7. */
  8. export class SassNumber extends Value {
  9. /**
  10. * Creates a new number with more complex units than just a single numerator.
  11. *
  12. * Upon construction, any compatible numerator and denominator units are
  13. * simplified away according to the conversion factor between them.
  14. *
  15. * @param value - The number's numeric value.
  16. *
  17. * @param unit - If this is a string, it's used as the single numerator unit
  18. * for the number.
  19. *
  20. * @param unit.numeratorUnits - If passed, these are the numerator units to
  21. * use for the number. This may be either a plain JavaScript array or an
  22. * immutable {@link List} from the [`immutable`
  23. * package](https://immutable-js.com/).
  24. *
  25. * @param unit.denominatorUnits - If passed, these are the denominator units
  26. * to use for the number. This may be either a plain JavaScript array or an
  27. * immutable {@link List} from the [`immutable`
  28. * package](https://immutable-js.com/).
  29. */
  30. constructor(
  31. value: number,
  32. unit?:
  33. | string
  34. | {
  35. numeratorUnits?: string[] | List<string>;
  36. denominatorUnits?: string[] | List<string>;
  37. }
  38. );
  39. /** This number's numeric value. */
  40. get value(): number;
  41. /** Whether {@link value} is an integer according to Sass's equality logic. */
  42. get isInt(): boolean;
  43. /**
  44. * If {@link value} is an integer according to {@link isInt}, returns {@link
  45. * value} rounded to that integer. If it's not an integer, returns `null`.
  46. */
  47. get asInt(): number | null;
  48. /**
  49. * This number's numerator units as an immutable {@link List} from the
  50. * [`immutable` package](https://immutable-js.com/).
  51. */
  52. get numeratorUnits(): List<string>;
  53. /**
  54. * This number's denominator units as an immutable {@link List} from the
  55. * [`immutable` package](https://immutable-js.com/).
  56. */
  57. get denominatorUnits(): List<string>;
  58. /** Whether this number has any numerator or denominator units. */
  59. get hasUnits(): boolean;
  60. /**
  61. * If {@link value} is an integer according to {@link isInt}, returns it
  62. * rounded to that integer. Otherwise, throws an error.
  63. *
  64. * @param name - The name of the function argument `this` came from (without
  65. * the `$`) if it came from an argument. Used for error reporting.
  66. */
  67. assertInt(name?: string): number;
  68. /**
  69. * Returns {@link value} if it's within `min` and `max`. If {@link value} is
  70. * equal to `min` or `max` according to Sass's equality, returns `min` or
  71. * `max` respectively. Otherwise, throws an error.
  72. *
  73. * @param name - The name of the function argument `this` came from (without
  74. * the `$`) if it came from an argument. Used for error reporting.
  75. */
  76. assertInRange(min: number, max: number, name?: string): number;
  77. /**
  78. * If this number has no units, returns it. Otherwise, throws an error.
  79. *
  80. * @param name - The name of the function argument `this` came from (without
  81. * the `$`) if it came from an argument. Used for error reporting.
  82. */
  83. assertNoUnits(name?: string): SassNumber;
  84. /**
  85. * If this number has `unit` as its only unit (and as a numerator), returns
  86. * this number. Otherwise, throws an error.
  87. *
  88. * @param name - The name of the function argument `this` came from (without
  89. * the `$`) if it came from an argument. Used for error reporting.
  90. */
  91. assertUnit(unit: string, name?: string): SassNumber;
  92. /** Whether this number has `unit` as its only unit (and as a numerator). */
  93. hasUnit(unit: string): boolean;
  94. /**
  95. * Whether this has exactly one numerator unit, and that unit is compatible
  96. * with `unit`.
  97. */
  98. compatibleWithUnit(unit: string): boolean;
  99. /**
  100. * Returns a copy of this number, converted to the units represented by
  101. * `newNumerators` and `newDenominators`.
  102. *
  103. * @throws `Error` if this number's units are incompatible with
  104. * `newNumerators` and `newDenominators`; or if this number is unitless and
  105. * either `newNumerators` or `newDenominators` are not empty, or vice-versa.
  106. *
  107. * @param newNumerators - The numerator units to convert this number to. This
  108. * may be either a plain JavaScript array or an immutable {@link List} from
  109. * the [`immutable` package](https://immutable-js.com/).
  110. *
  111. * @param newDenominators - The denominator units to convert this number to.
  112. * This may be either a plain JavaScript array or an immutable {@link List}
  113. * from the [`immutable` package](https://immutable-js.com/).
  114. *
  115. * @param name - The name of the function argument `this` came from (without
  116. * the `$`) if it came from an argument. Used for error reporting.
  117. */
  118. convert(
  119. newNumerators: string[] | List<string>,
  120. newDenominators: string[] | List<string>,
  121. name?: string
  122. ): SassNumber;
  123. /**
  124. * Returns a copy of this number, converted to the same units as `other`.
  125. *
  126. * @throws `Error` if this number's units are incompatible with `other`'s
  127. * units, or if either number is unitless but the other is not.
  128. *
  129. * @param name - The name of the function argument `this` came from (without
  130. * the `$`) if it came from an argument. Used for error reporting.
  131. *
  132. * @param otherName - The name of the function argument `other` came from
  133. * (without the `$`) if it came from an argument. Used for error reporting.
  134. */
  135. convertToMatch(
  136. other: SassNumber,
  137. name?: string,
  138. otherName?: string
  139. ): SassNumber;
  140. /**
  141. * Returns {@link value}, converted to the units represented by
  142. * `newNumerators` and `newDenominators`.
  143. *
  144. * @throws `Error` if this number's units are incompatible with
  145. * `newNumerators` and `newDenominators`; or if this number is unitless and
  146. * either `newNumerators` or `newDenominators` are not empty, or vice-versa.
  147. *
  148. * @param newNumerators - The numerator units to convert {@link value} to.
  149. * This may be either a plain JavaScript array or an immutable {@link List}
  150. * from the [`immutable` package](https://immutable-js.com/).
  151. *
  152. * @param newDenominators - The denominator units to convert {@link value} to.
  153. * This may be either a plain JavaScript array or an immutable {@link List}
  154. * from the [`immutable` package](https://immutable-js.com/).
  155. *
  156. * @param name - The name of the function argument `this` came from (without
  157. * the `$`) if it came from an argument. Used for error reporting.
  158. */
  159. convertValue(
  160. newNumerators: string[] | List<string>,
  161. newDenominators: string[] | List<string>,
  162. name?: string
  163. ): number;
  164. /**
  165. * Returns {@link value}, converted to the same units as `other`.
  166. *
  167. * @throws `Error` if this number's units are incompatible with `other`'s
  168. * units, or if either number is unitless but the other is not.
  169. *
  170. * @param name - The name of the function argument `this` came from (without
  171. * the `$`) if it came from an argument. Used for error reporting.
  172. *
  173. * @param otherName - The name of the function argument `other` came from
  174. * (without the `$`) if it came from an argument. Used for error reporting.
  175. */
  176. convertValueToMatch(
  177. other: SassNumber,
  178. name?: string,
  179. otherName?: string
  180. ): number;
  181. /**
  182. * Returns a copy of this number, converted to the units represented by
  183. * `newNumerators` and `newDenominators`.
  184. *
  185. * Unlike {@link convert} this does *not* throw an error if this number is
  186. * unitless and either `newNumerators` or `newDenominators` are not empty, or
  187. * vice-versa. Instead, it treats all unitless numbers as convertible to and
  188. * from all units without changing the value.
  189. *
  190. * @throws `Error` if this number's units are incompatible with
  191. * `newNumerators` and `newDenominators`.
  192. *
  193. * @param newNumerators - The numerator units to convert this number to. This
  194. * may be either a plain JavaScript array or an immutable {@link List} from
  195. * the [`immutable` package](https://immutable-js.com/).
  196. *
  197. * @param newDenominators - The denominator units to convert this number to.
  198. * This may be either a plain JavaScript array or an immutable {@link List}
  199. * from the [`immutable` package](https://immutable-js.com/).
  200. *
  201. * @param name - The name of the function argument `this` came from (without
  202. * the `$`) if it came from an argument. Used for error reporting.
  203. */
  204. coerce(
  205. newNumerators: string[] | List<string>,
  206. newDenominators: string[] | List<string>,
  207. name?: string
  208. ): SassNumber;
  209. /**
  210. * Returns a copy of this number, converted to the units represented by
  211. * `newNumerators` and `newDenominators`.
  212. *
  213. * Unlike {@link convertToMatch} this does *not* throw an error if this number
  214. * is unitless and either `newNumerators` or `newDenominators` are not empty,
  215. * or vice-versa. Instead, it treats all unitless numbers as convertible to
  216. * and from all units without changing the value.
  217. *
  218. * @throws `Error` if this number's units are incompatible with `other`'s
  219. * units.
  220. *
  221. * @param name - The name of the function argument `this` came from (without
  222. * the `$`) if it came from an argument. Used for error reporting.
  223. *
  224. * @param otherName - The name of the function argument `other` came from
  225. * (without the `$`) if it came from an argument. Used for error reporting.
  226. */
  227. coerceToMatch(
  228. other: SassNumber,
  229. name?: string,
  230. otherName?: string
  231. ): SassNumber;
  232. /**
  233. * Returns {@link value}, converted to the units represented by
  234. * `newNumerators` and `newDenominators`.
  235. *
  236. * Unlike {@link convertValue} this does *not* throw an error if this number
  237. * is unitless and either `newNumerators` or `newDenominators` are not empty,
  238. * or vice-versa. Instead, it treats all unitless numbers as convertible to
  239. * and from all units without changing the value.
  240. *
  241. * @throws `Error` if this number's units are incompatible with
  242. * `newNumerators` and `newDenominators`.
  243. *
  244. * @param newNumerators - The numerator units to convert {@link value} to.
  245. * This may be either a plain JavaScript array or an immutable {@link List}
  246. * from the [`immutable` package](https://immutable-js.com/).
  247. *
  248. * @param newDenominators - The denominator units to convert {@link value} to.
  249. * This may be either a plain JavaScript array or an immutable {@link List}
  250. * from the [`immutable` package](https://immutable-js.com/).
  251. *
  252. * @param name - The name of the function argument `this` came from (without
  253. * the `$`) if it came from an argument. Used for error reporting.
  254. */
  255. coerceValue(
  256. newNumerators: string[] | List<string>,
  257. newDenominators: string[] | List<string>,
  258. name?: string
  259. ): number;
  260. /**
  261. * Returns {@link value}, converted to the units represented by
  262. * `newNumerators` and `newDenominators`.
  263. *
  264. * Unlike {@link convertValueToMatch} this does *not* throw an error if this
  265. * number is unitless and either `newNumerators` or `newDenominators` are not
  266. * empty, or vice-versa. Instead, it treats all unitless numbers as
  267. * convertible to and from all units without changing the value.
  268. *
  269. * @throws `Error` if this number's units are incompatible with `other`'s
  270. * units.
  271. *
  272. * @param name - The name of the function argument `this` came from (without
  273. * the `$`) if it came from an argument. Used for error reporting.
  274. *
  275. * @param otherName - The name of the function argument `other` came from
  276. * (without the `$`) if it came from an argument. Used for error reporting.
  277. */
  278. coerceValueToMatch(
  279. other: SassNumber,
  280. name?: string,
  281. otherName?: string
  282. ): number;
  283. }