This file contains reusable code blocks that can be imported by other org files using the org block import feature.
JavaScript Utilities
/**
* Reusable math utilities
*/
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
export function fibonacci(n) {
if (n <= 1) return n;
let a = 0, b = 1;
for (let i = 2; i <= n; i++) {
[a, b] = [b, a + b];
}
return b;
}
Source
/**
* Reusable math utilities
*/
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
export function fibonacci(n) {
if (n <= 1) return n;
let a = 0, b = 1;
for (let i = 2; i <= n; i++) {
[a, b] = [b, a + b];
}
return b;
}
/**
* String manipulation utilities
*/
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export function slugify(str) {
return str
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
}
export function truncate(str, maxLength) {
if (str.length <= maxLength) return str;
return str.slice(0, maxLength - 3) + '...';
}
Source
/**
* String manipulation utilities
*/
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export function slugify(str) {
return str
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
}
export function truncate(str, maxLength) {
if (str.length <= maxLength) return str;
return str.slice(0, maxLength - 3) + '...';
}
CSS Utilities
/* Flexbox utility classes */
.flex-center {
display: flex;
justify-content: center;
align-items: center;
}
.flex-column {
display: flex;
flex-direction: column;
}
.flex-between {
display: flex;
justify-content: space-between;
align-items: center;
}
.flex-gap-1 {
gap: 0.25rem;
}
.flex-gap-2 {
gap: 0.5rem;
}
.flex-gap-4 {
gap: 1rem;
}
/* Animation utilities */
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.fade-in {
animation: fadeIn 0.3s ease-in-out;
}
.spin {
animation: spin 1s linear infinite;
}
JSCad Geometry Primitives
import * as jscad from '@jscad/modeling';
const { cube, sphere, cylinder, cuboid } = jscad.primitives;
const { translate, scale, rotate } = jscad.transforms;
const { union, subtract, intersect } = jscad.booleans;
const { colorize } = jscad.colors;
/**
* Create a rounded cube
*/
export function roundedCube(size = 10, radius = 2) {
const mainCube = cuboid({ size: [size, size, size] });
const corners = [];
const offset = size / 2 - radius;
for (let x of [-1, 1]) {
for (let y of [-1, 1]) {
for (let z of [-1, 1]) {
corners.push(
translate(
[x * offset, y * offset, z * offset],
sphere({ radius })
)
);
}
}
}
return union(mainCube, ...corners);
}
/**
* Create a simple house shape
*/
export function house(width = 20, height = 15) {
const base = cuboid({ size: [width, width, height] });
const roof = translate(
[0, 0, height / 2],
rotate(
[Math.PI / 4, 0, 0],
cuboid({ size: [width, width * 1.4, 2] })
)
);
return colorize([0.8, 0.6, 0.4], union(base, roof));
}
/**
* Create a gear tooth
*/
export function gear(teeth = 12, innerRadius = 10, outerRadius = 15, thickness = 3) {
const base = cylinder({ radius: innerRadius, height: thickness });
const toothAngle = (2 * Math.PI) / teeth;
const teethShapes = [];
for (let i = 0; i < teeth; i++) {
const angle = i * toothAngle;
const tooth = translate(
[Math.cos(angle) * (innerRadius + outerRadius) / 2,
Math.sin(angle) * (innerRadius + outerRadius) / 2,
0],
rotate(
[0, 0, angle],
cuboid({ size: [outerRadius - innerRadius, 2, thickness] })
)
);
teethShapes.push(tooth);
}
return colorize([0.7, 0.7, 0.8], union(base, ...teethShapes));
}
Source
import * as jscad from '@jscad/modeling';
const { cube, sphere, cylinder, cuboid } = jscad.primitives;
const { translate, scale, rotate } = jscad.transforms;
const { union, subtract, intersect } = jscad.booleans;
const { colorize } = jscad.colors;
/**
* Create a rounded cube
*/
export function roundedCube(size = 10, radius = 2) {
const mainCube = cuboid({ size: [size, size, size] });
const corners = [];
const offset = size / 2 - radius;
for (let x of [-1, 1]) {
for (let y of [-1, 1]) {
for (let z of [-1, 1]) {
corners.push(
translate(
[x * offset, y * offset, z * offset],
sphere({ radius })
)
);
}
}
}
return union(mainCube, ...corners);
}
/**
* Create a simple house shape
*/
export function house(width = 20, height = 15) {
const base = cuboid({ size: [width, width, height] });
const roof = translate(
[0, 0, height / 2],
rotate(
[Math.PI / 4, 0, 0],
cuboid({ size: [width, width * 1.4, 2] })
)
);
return colorize([0.8, 0.6, 0.4], union(base, roof));
}
/**
* Create a gear tooth
*/
export function gear(teeth = 12, innerRadius = 10, outerRadius = 15, thickness = 3) {
const base = cylinder({ radius: innerRadius, height: thickness });
const toothAngle = (2 * Math.PI) / teeth;
const teethShapes = [];
for (let i = 0; i < teeth; i++) {
const angle = i * toothAngle;
const tooth = translate(
[Math.cos(angle) * (innerRadius + outerRadius) / 2,
Math.sin(angle) * (innerRadius + outerRadius) / 2,
0],
rotate(
[0, 0, angle],
cuboid({ size: [outerRadius - innerRadius, 2, thickness] })
)
);
teethShapes.push(tooth);
}
return colorize([0.7, 0.7, 0.8], union(base, ...teethShapes));
}
import * as jscad from '@jscad/modeling';
const { cube, sphere, cylinder } = jscad.primitives;
const { translate, rotate } = jscad.transforms;
const { union, subtract } = jscad.booleans;
const { colorize } = jscad.colors;
/**
* Create a simple screw
*/
export function screw(length = 30, diameter = 6, headDiameter = 10) {
const shaft = cylinder({ radius: diameter / 2, height: length });
const head = translate(
[0, 0, length / 2],
cylinder({ radius: headDiameter / 2, height: 3 })
);
return colorize([0.6, 0.6, 0.6], union(shaft, head));
}
/**
* Create a bearing
*/
export function bearing(outerD = 20, innerD = 10, thickness = 5) {
const outer = cylinder({ radius: outerD / 2, height: thickness });
const inner = cylinder({ radius: innerD / 2, height: thickness + 1 });
return colorize([0.5, 0.5, 0.6], subtract(outer, inner));
}
Source
import * as jscad from '@jscad/modeling';
const { cube, sphere, cylinder } = jscad.primitives;
const { translate, rotate } = jscad.transforms;
const { union, subtract } = jscad.booleans;
const { colorize } = jscad.colors;
/**
* Create a simple screw
*/
export function screw(length = 30, diameter = 6, headDiameter = 10) {
const shaft = cylinder({ radius: diameter / 2, height: length });
const head = translate(
[0, 0, length / 2],
cylinder({ radius: headDiameter / 2, height: 3 })
);
return colorize([0.6, 0.6, 0.6], union(shaft, head));
}
/**
* Create a bearing
*/
export function bearing(outerD = 20, innerD = 10, thickness = 5) {
const outer = cylinder({ radius: outerD / 2, height: thickness });
const inner = cylinder({ radius: innerD / 2, height: thickness + 1 });
return colorize([0.5, 0.5, 0.6], subtract(outer, inner));
}