-
Notifications
You must be signed in to change notification settings - Fork 29
Add support for SassCalculation #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
02684ec
Add support for SassCalculation
jerivas 473a938
Lint
jerivas 53de5e5
Add legal notice
jerivas a17f1e0
Expose calculation types
jerivas cd6a2b9
Enforce operator validation
jerivas ae1c7f3
Address review
jerivas 4e19108
Update after spec changes
jerivas 3cb3318
WIP result simplification
jerivas 51e2f7b
Lint
jerivas ba077ee
Address review
jerivas ced9d56
Add proto logic for calculations
jerivas 22af422
Lint
jerivas 8d2d5a0
No need to simplify in the host
jerivas f6641d6
Add missing comment
jerivas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import {hash, List, ValueObject} from 'immutable'; | ||
|
||
import {Value} from './index'; | ||
import {SassNumber} from './number'; | ||
import {SassString} from './string'; | ||
|
||
export type CalculationValue = | ||
| SassNumber | ||
| SassCalculation | ||
| SassString | ||
| CalculationOperation | ||
| CalculationInterpolation; | ||
|
||
type CalculationValueIterable = CalculationValue[] | List<CalculationValue>; | ||
|
||
function checkUnquotedString(value: CalculationValue): void { | ||
if (value instanceof SassString && value.hasQuotes) { | ||
throw new Error(`Expected ${value} to be an unquoted string.`); | ||
} | ||
} | ||
|
||
export class SassCalculation extends Value { | ||
readonly name: string; | ||
readonly arguments: List<CalculationValue>; | ||
|
||
private constructor(name: string, args: CalculationValueIterable) { | ||
super(); | ||
this.name = name; | ||
this.arguments = List(args); | ||
} | ||
|
||
static calc(argument: CalculationValue): SassCalculation { | ||
checkUnquotedString(argument); | ||
return new SassCalculation('calc', [argument]); | ||
} | ||
|
||
static min(args: CalculationValueIterable): SassCalculation { | ||
args.forEach(checkUnquotedString); | ||
return new SassCalculation('min', args); | ||
} | ||
|
||
static max(args: CalculationValueIterable): SassCalculation { | ||
args.forEach(checkUnquotedString); | ||
return new SassCalculation('max', args); | ||
} | ||
|
||
static clamp( | ||
min: CalculationValue, | ||
value?: CalculationValue, | ||
max?: CalculationValue | ||
): SassCalculation { | ||
if (value === undefined && max === undefined) { | ||
let minString: string; | ||
if (min instanceof SassString) { | ||
minString = min.text; | ||
} else if (min instanceof CalculationInterpolation) { | ||
minString = min.value; | ||
} else { | ||
throw new Error( | ||
'`value` and `max` are both undefined, but `min` is not a SassString or CalculationInterpolation.' | ||
); | ||
} | ||
const values = minString.split(',').map(s => { | ||
const parsed = parseFloat(s.trim()); | ||
return isNaN(parsed) ? undefined : new SassNumber(parsed); | ||
}); | ||
const error = new Error( | ||
`Expected \`min\` to be a comma-separated list of numbers, got \`${min}\`` | ||
); | ||
jerivas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (values[0] === undefined) { | ||
throw error; | ||
} else if (values.length === 2) { | ||
[min, value] = values; | ||
} else if (values.length === 3) { | ||
[min, value, max] = values; | ||
} else { | ||
throw error; | ||
} | ||
} else if (value === undefined && max !== undefined) { | ||
throw new Error('`value` is undefined but `max` is defined.'); | ||
} | ||
const args = [min]; | ||
if (value !== undefined) args.push(value); | ||
if (max !== undefined) args.push(max); | ||
args.forEach(checkUnquotedString); | ||
return new SassCalculation('clamp', args); | ||
} | ||
|
||
assertCalculation(): SassCalculation { | ||
return this; | ||
} | ||
|
||
equals(other: Value): boolean { | ||
return ( | ||
other instanceof SassCalculation && | ||
this.name === other.name && | ||
this.arguments.equals(other.arguments) | ||
); | ||
} | ||
|
||
hashCode(): number { | ||
return hash(this.name) ^ this.arguments.hashCode(); | ||
} | ||
|
||
toString(): string { | ||
return `${this.name}(${this.arguments.join(', ')})`; | ||
} | ||
} | ||
|
||
export type CalculationOperator = '+' | '-' | '*' | '/'; | ||
|
||
export class CalculationOperation implements ValueObject { | ||
readonly operator: CalculationOperator; | ||
readonly left: CalculationValue; | ||
readonly right: CalculationValue; | ||
|
||
constructor( | ||
operator: CalculationOperator, | ||
left: CalculationValue, | ||
right: CalculationValue | ||
jerivas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) { | ||
this.operator = operator; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
equals(other: Value): boolean { | ||
return ( | ||
other instanceof CalculationOperation && | ||
this.operator === other.operator && | ||
this.left === other.left && | ||
this.right === other.right | ||
); | ||
} | ||
|
||
hashCode(): number { | ||
return hash(this.operator) ^ hash(this.left) ^ hash(this.right); | ||
} | ||
} | ||
|
||
export class CalculationInterpolation implements ValueObject { | ||
readonly value: string; | ||
|
||
constructor(value: string) { | ||
this.value = value; | ||
} | ||
|
||
equals(other: Value): boolean { | ||
return ( | ||
other instanceof CalculationInterpolation && this.value === other.value | ||
); | ||
} | ||
|
||
hashCode(): number { | ||
return hash(this.value); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.