UNPKG

@tonaljs/time-signature

Version:

Musical time signatures

92 lines (91 loc) 2.81 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var time_signature_exports = {}; __export(time_signature_exports, { default: () => time_signature_default, get: () => get, names: () => names, parse: () => parse }); module.exports = __toCommonJS(time_signature_exports); var NONE = { empty: true, name: "", upper: void 0, lower: void 0, type: void 0, additive: [] }; var NAMES = ["4/4", "3/4", "2/4", "2/2", "12/8", "9/8", "6/8", "3/8"]; function names() { return NAMES.slice(); } var REGEX = /^(\d*\d(?:\+\d)*)\/(\d+)$/; var CACHE = /* @__PURE__ */ new Map(); function get(literal) { const stringifiedLiteral = JSON.stringify(literal); const cached = CACHE.get(stringifiedLiteral); if (cached) { return cached; } const ts = build(parse(literal)); CACHE.set(stringifiedLiteral, ts); return ts; } function parse(literal) { if (typeof literal === "string") { const [_, up2, low] = REGEX.exec(literal) || []; return parse([up2, low]); } const [up, down] = literal; const denominator = +down; if (typeof up === "number") { return [up, denominator]; } const list = up.split("+").map((n) => +n); return list.length === 1 ? [list[0], denominator] : [list, denominator]; } var time_signature_default = { names, parse, get }; var isPowerOfTwo = (x) => Math.log(x) / Math.log(2) % 1 === 0; function build([up, down]) { const upper = Array.isArray(up) ? up.reduce((a, b) => a + b, 0) : up; const lower = down; if (upper === 0 || lower === 0) { return NONE; } const name = Array.isArray(up) ? `${up.join("+")}/${down}` : `${up}/${down}`; const additive = Array.isArray(up) ? up : []; const type = lower === 4 || lower === 2 ? "simple" : lower === 8 && upper % 3 === 0 ? "compound" : isPowerOfTwo(lower) ? "irregular" : "irrational"; return { empty: false, name, type, upper, lower, additive }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { get, names, parse }); //# sourceMappingURL=index.js.map