Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Commit aa4161a

Browse files
committed
ADDED: Initial commit
0 parents commit aa4161a

File tree

10 files changed

+199
-0
lines changed

10 files changed

+199
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
*.lock

functions.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import "functions/convert-unit";
2+
@import "functions/modular-scale";
3+
@import "functions/rem-scale";
4+
@import "functions/em-scale";

functions/_convert-unit.scss

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@function convert-unit($value, $unit) {
2+
$units: (
3+
"px": 0px,
4+
"cm": 0cm,
5+
"mm": 0mm,
6+
"%": 0%,
7+
"ch": 0ch,
8+
"in": 0in,
9+
"em": 0em,
10+
"rem": 0rem,
11+
"pt": 0pt,
12+
"pc": 0pc,
13+
"ex": 0ex,
14+
"vw": 0vw,
15+
"vh": 0vh,
16+
"vmin": 0vmin,
17+
"vmax": 0vmax,
18+
"deg": 0deg,
19+
"turn": 0turn,
20+
"rad": 0rad,
21+
"grad": 0grad,
22+
"s": 0s,
23+
"ms": 0ms,
24+
"Hz": 0hz,
25+
"kHz": 0khz,
26+
"dppx": 0dppx,
27+
"dpcm": 0dpcm,
28+
"dpi": 0dpi
29+
);
30+
31+
@if map-has-key($units, $unit) {
32+
@return map-get($units, $unit) + $value;
33+
}
34+
35+
@error "Cannot convert '#{$value}' to '#{$unit}'";
36+
}

functions/_em-scale.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$em-scale-ratio: 1.2 !default;
2+
3+
@function em-scale($increment: 0, $ratio: $em-scale-ratio) {
4+
@return modular-scale(1, $increment, $ratio, "em");
5+
}

functions/_modular-scale.scss

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
$modular-scale-ratio: 2 !default;
2+
3+
@function modular-scale(
4+
$value,
5+
$increment: 0,
6+
$ratio: $modular-scale-ratio,
7+
$unit: ""
8+
) {
9+
// Error if $value is not a number or has units
10+
@if (type-of($value) != "number") {
11+
@error "$value should be a number, '#{$value}' (#{type-of($value)}) supplied";
12+
}
13+
14+
// Error if $increment is not a number or has units
15+
@if (type-of($increment) != "number" or not unitless($increment)) {
16+
@error "$increment should be a unitless number, '#{$increment}' (#{type-of($increment)}) supplied";
17+
}
18+
19+
// Error if $ratio is not a number or has units
20+
@if (type-of($ratio) != "number" or not unitless($ratio)) {
21+
@error "$ratio should be a unitless number, '#{$ratio}' (#{type-of($ratio)}) supplied";
22+
}
23+
24+
// Error if $unit is not a string
25+
@if (type-of($unit) != "string") {
26+
@error "$unit should be a string, '#{$unit}' (#{type-of($unit)}) supplied";
27+
}
28+
29+
// Keep the original unit type if not set manually
30+
@if (not unitless($value) and $unit == "") {
31+
$unit: unit($value);
32+
}
33+
34+
// Strip units from $value
35+
$value: $value / ($value * 0 + 1);
36+
37+
// Calculate positive increments
38+
@if ($increment > 0) {
39+
@for $i from 1 through $increment {
40+
$value: $value * $ratio;
41+
}
42+
}
43+
44+
// Calculate negative increments
45+
@if ($increment < 0) {
46+
@for $i from 1 through abs($increment) {
47+
$value: $value / $ratio;
48+
}
49+
}
50+
51+
// Convert $value to specified $unit and return
52+
@if ($unit != "") {
53+
@return convert-unit($value, $unit);
54+
}
55+
56+
@return $value;
57+
}

functions/_rem-scale.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$rem-scale-ratio: 1.2 !default;
2+
3+
@function rem-scale($increment: 0, $ratio: $rem-scale-ratio) {
4+
@return modular-scale(1, $increment, $ratio, "rem");
5+
}

gulpfile.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
let gulp = require("gulp");
4+
let sass = require("gulp-sass");
5+
let plumber = require("gulp-plumber");
6+
7+
gulp.task("scss", () => {
8+
return gulp
9+
.src(["tests/tests.scss"])
10+
.pipe(plumber())
11+
.pipe(sass())
12+
.pipe(gulp.dest("tests/"));
13+
});
14+
15+
gulp.task("watch", () => {
16+
gulp.watch("tests/tests.scss", ["scss"]);
17+
});
18+
19+
gulp.task("default", ["scss", "watch"]);

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "modular-scale-scss-functions",
3+
"version": "0.0.1",
4+
"description":
5+
"A modular scale SCSS function with a few complimentary helper functions",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+ssh://git@github.com/boov/modular-scale-scss-functions.git"
9+
},
10+
"keywords": ["modular", "scale", "scss", "functions"],
11+
"author": "Matthew Booth <matt@boov.co.uk>",
12+
"license": "ISC",
13+
"bugs": {
14+
"url": "https://github.com/boov/modular-scale-scss-functions/issues"
15+
},
16+
"homepage": "https://github.com/boov/modular-scale-scss-functions#readme",
17+
"devDependencies": {
18+
"gulp": "^3.9.1",
19+
"gulp-plumber": "^1.2.0",
20+
"gulp-sass": "^3.1.0"
21+
}
22+
}

tests/tests.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.results {
2+
--should-return-1: 1;
3+
--should-return-1em: 1em;
4+
--should-return-2: 2;
5+
--should-return-4px: 4px;
6+
--should-return-4: 4;
7+
--should-return-8rem: 8rem;
8+
--should-return-16em: 16em;
9+
--should-return-32px: 32px;
10+
--should-return-1-point-2rem: 1.2rem;
11+
--should-return-1-point-44rem: 1.44rem;
12+
--should-return-2rem: 2rem;
13+
--should-return-4rem: 4rem;
14+
--should-return-1-point-2em: 1.2em;
15+
--should-return-1-point-44em: 1.44em;
16+
--should-return-2em: 2em;
17+
--should-return-4em: 4em; }

tests/tests.scss

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@import "../functions.scss";
2+
3+
.results {
4+
--should-return-1: modular-scale(1);
5+
--should-return-1em: modular-scale(1em);
6+
7+
--should-return-2: modular-scale(2, 0);
8+
--should-return-4px: modular-scale(2px, 1);
9+
10+
--should-return-4: modular-scale(4, 1, 1);
11+
--should-return-8rem: modular-scale(4rem, 1, 2);
12+
13+
--should-return-16em: modular-scale(8, 1, 2, "em");
14+
--should-return-32px: modular-scale(8%, 2, 2, "px");
15+
16+
--should-return-1-point-2rem: rem-scale(1);
17+
--should-return-1-point-44rem: rem-scale(2);
18+
19+
--should-return-2rem: rem-scale(1, 2);
20+
--should-return-4rem: rem-scale(2, 2);
21+
22+
--should-return-1-point-2em: em-scale(1);
23+
--should-return-1-point-44em: em-scale(2);
24+
25+
--should-return-2em: em-scale(1, 2);
26+
--should-return-4em: em-scale(2, 2);
27+
28+
//--should-value-error: modular-scale(#fff);
29+
//--should-increment-error: modular-scale(1, #fff);
30+
//--should-ratio-error: modular-scale(1, 1, #fff);
31+
//--should-unit-error: modular-scale(1, 1, 1, #fff);
32+
}

0 commit comments

Comments
 (0)