chordsheetjs 5.3.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/chord.js CHANGED
@@ -12,6 +12,8 @@ var _utilities = require("./utilities");
12
12
 
13
13
  var _key = _interopRequireDefault(require("./key"));
14
14
 
15
+ var _suffixNormalizeMapping = _interopRequireDefault(require("./normalize_mappings/suffix-normalize-mapping"));
16
+
15
17
  var _constants = require("./constants");
16
18
 
17
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
@@ -36,12 +38,6 @@ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollect
36
38
 
37
39
  function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
38
40
 
39
- function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
40
-
41
- function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
42
-
43
- function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
44
-
45
41
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
46
42
 
47
43
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -60,23 +56,12 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
60
56
 
61
57
  function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
62
58
 
63
- var MAJOR_SCALE = [null, 'M', 'm', 'm', 'M', 'M', 'm', 'dim'];
64
-
65
59
  function normalizeSuffix(suffix) {
66
- if (suffix === 'M') {
67
- return '';
60
+ if (_suffixNormalizeMapping["default"][suffix] === '[blank]') {
61
+ return null;
68
62
  }
69
63
 
70
- return suffix;
71
- }
72
-
73
- function chordSuffix(noteNumber, suffix) {
74
- if ((0, _utilities.isEmptyString)(suffix)) {
75
- var defaultSuffix = MAJOR_SCALE[noteNumber];
76
- return normalizeSuffix(defaultSuffix);
77
- }
78
-
79
- return normalizeSuffix(suffix);
64
+ return _suffixNormalizeMapping["default"][suffix] || suffix;
80
65
  }
81
66
 
82
67
  var chordRegex = /*#__PURE__*/_wrapRegExp(/^([A-G])(#|b)?((?:(?![\t-\r \/\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])[\s\S])*)(\/([A-G])(#|b)?)?$/i, {
@@ -143,15 +128,24 @@ var Chord = /*#__PURE__*/function () {
143
128
  set: void 0
144
129
  });
145
130
 
131
+ this.suffix = (0, _utilities.presence)(suffix);
146
132
  this.root = root || new _key["default"]({
147
133
  note: base,
148
- modifier: modifier
134
+ modifier: modifier,
135
+ minor: suffix === 'm'
149
136
  });
150
- this.suffix = (0, _utilities.presence)(suffix);
151
- this.bass = bass || (bassBase ? new _key["default"]({
152
- note: bassBase,
153
- modifier: bassModifier
154
- }) : null);
137
+
138
+ if (bass) {
139
+ this.bass = bass;
140
+ } else if (bassBase) {
141
+ this.bass = new _key["default"]({
142
+ note: bassBase,
143
+ modifier: bassModifier,
144
+ minor: suffix === 'm'
145
+ });
146
+ } else {
147
+ this.bass = null;
148
+ }
155
149
  }
156
150
 
157
151
  _createClass(Chord, [{
@@ -183,10 +177,11 @@ var Chord = /*#__PURE__*/function () {
183
177
 
184
178
  var keyObj = _key["default"].wrap(key);
185
179
 
180
+ var rootKey = this.root.toChordSymbol(keyObj).normalizeEnharmonics(keyObj);
186
181
  var chordSymbolChord = new Chord({
187
- suffix: chordSuffix(_classPrivateFieldGet(this, _rootNote), this.suffix),
188
- root: this.root.toChordSymbol(keyObj),
189
- bass: (_this$bass = this.bass) === null || _this$bass === void 0 ? void 0 : _this$bass.toChordSymbol(keyObj)
182
+ suffix: normalizeSuffix(this.suffix),
183
+ root: rootKey,
184
+ bass: (_this$bass = this.bass) === null || _this$bass === void 0 ? void 0 : _this$bass.toChordSymbol(keyObj).normalizeEnharmonics(rootKey)
190
185
  });
191
186
 
192
187
  if (this.root.isMinor()) {
@@ -247,7 +242,7 @@ var Chord = /*#__PURE__*/function () {
247
242
  var keyObj = _key["default"].wrap(key);
248
243
 
249
244
  return new Chord({
250
- suffix: chordSuffix(_classPrivateFieldGet(this, _rootNote), this.suffix),
245
+ suffix: normalizeSuffix(this.suffix),
251
246
  root: this.root.toNumeric(keyObj),
252
247
  bass: (_this$bass3 = this.bass) === null || _this$bass3 === void 0 ? void 0 : _this$bass3.toNumeric(keyObj)
253
248
  });
@@ -282,7 +277,7 @@ var Chord = /*#__PURE__*/function () {
282
277
  var keyObj = _key["default"].wrap(key);
283
278
 
284
279
  return new Chord({
285
- suffix: chordSuffix(_classPrivateFieldGet(this, _rootNote), this.suffix),
280
+ suffix: normalizeSuffix(this.suffix),
286
281
  root: this.root.toNumeral(keyObj),
287
282
  bass: (_this$bass5 = this.bass) === null || _this$bass5 === void 0 ? void 0 : _this$bass5.toNumeral(keyObj)
288
283
  });
@@ -350,7 +345,7 @@ var Chord = /*#__PURE__*/function () {
350
345
  return chordString;
351
346
  }
352
347
  /**
353
- * Normalizes the chord:
348
+ * Normalizes the chord root and bass notes:
354
349
  * - Fb becomes E
355
350
  * - Cb becomes B
356
351
  * - B# becomes C
@@ -360,14 +355,26 @@ var Chord = /*#__PURE__*/function () {
360
355
  * - 7# becomes 1
361
356
  * - 3# becomes 4
362
357
  *
363
- * If the chord is already normalized, this will return a copy.
358
+ * Besides that it normalizes the suffix. For example, `sus2` becomes `2`, `sus4` becomes `sus`.
359
+ * All suffix normalizations can be found in `src/normalize_mappings/suffix-mapping.txt`.
360
+ *
364
361
  * @returns {Chord} the normalized chord
365
362
  */
366
363
 
367
364
  }, {
368
365
  key: "normalize",
369
- value: function normalize() {
370
- return _classPrivateMethodGet(this, _process, _process2).call(this, 'normalize');
366
+ value: function normalize(key) {
367
+ if (!(0, _utilities.presence)(key)) {
368
+ return _classPrivateMethodGet(this, _process, _process2).call(this, 'normalize').set({
369
+ suffix: (0, _utilities.presence)(normalizeSuffix(this.suffix))
370
+ });
371
+ }
372
+
373
+ return this.set({
374
+ root: this.root.normalizeEnharmonics(key),
375
+ suffix: (0, _utilities.presence)(normalizeSuffix(this.suffix)),
376
+ bass: this.bass ? this.bass.normalizeEnharmonics(this.root.toString()) : null
377
+ });
371
378
  }
372
379
  /**
373
380
  * Switches to the specified modifier
package/lib/key.js CHANGED
@@ -11,6 +11,8 @@ var _note = _interopRequireDefault(require("./note"));
11
11
 
12
12
  var _constants = require("./constants");
13
13
 
14
+ var _enharmonicNormalize = _interopRequireDefault(require("./normalize_mappings/enharmonic-normalize"));
15
+
14
16
  var _MODIFIER_TRANSPOSITI;
15
17
 
16
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
@@ -43,14 +45,16 @@ var FLAT = 'b';
43
45
  var SHARP = '#';
44
46
  var MODIFIER_TRANSPOSITION = (_MODIFIER_TRANSPOSITI = {}, _defineProperty(_MODIFIER_TRANSPOSITI, SHARP, 1), _defineProperty(_MODIFIER_TRANSPOSITI, FLAT, -1), _MODIFIER_TRANSPOSITI);
45
47
 
46
- var symbolKeyRegex = /*#__PURE__*/_wrapRegExp(/^([A-G])(#|b)?$/i, {
48
+ var symbolKeyRegex = /*#__PURE__*/_wrapRegExp(/^([A-G])(#|b)?(m)?$/i, {
47
49
  note: 1,
48
- modifier: 2
50
+ modifier: 2,
51
+ minor: 3
49
52
  });
50
53
 
51
- var numericKeyRegex = /*#__PURE__*/_wrapRegExp(/^(#|b)?([1-7])$/, {
54
+ var numericKeyRegex = /*#__PURE__*/_wrapRegExp(/^(#|b)?([1-7])(m)?$/, {
52
55
  modifier: 1,
53
- note: 2
56
+ note: 2,
57
+ minor: 3
54
58
  });
55
59
 
56
60
  var numeralKeyRegex = /*#__PURE__*/_wrapRegExp(/^(#|b)?(I{1,3}|IV|VI{0,2}|i{1,3}|iv|vi{0,2})$/, {
@@ -66,7 +70,9 @@ var Key = /*#__PURE__*/function () {
66
70
  function Key(_ref) {
67
71
  var note = _ref.note,
68
72
  _ref$modifier = _ref.modifier,
69
- modifier = _ref$modifier === void 0 ? null : _ref$modifier;
73
+ modifier = _ref$modifier === void 0 ? null : _ref$modifier,
74
+ _ref$minor = _ref.minor,
75
+ minor = _ref$minor === void 0 ? false : _ref$minor;
70
76
 
71
77
  _classCallCheck(this, Key);
72
78
 
@@ -74,6 +80,7 @@ var Key = /*#__PURE__*/function () {
74
80
 
75
81
  this.note = note instanceof _note["default"] ? note : _note["default"].parse(note);
76
82
  this.modifier = modifier || null;
83
+ this.minor = !!minor || false;
77
84
  }
78
85
 
79
86
  _createClass(Key, [{
@@ -100,6 +107,21 @@ var Key = /*#__PURE__*/function () {
100
107
  value: function clone() {
101
108
  return _classPrivateMethodGet(this, _set, _set2).call(this, {});
102
109
  }
110
+ }, {
111
+ key: "toChordSymbol",
112
+ value: function toChordSymbol(key) {
113
+ if (this.is(_constants.SYMBOL)) {
114
+ return this.clone();
115
+ }
116
+
117
+ var transposeDistance = this.note.getTransposeDistance(key.minor) + (MODIFIER_TRANSPOSITION[this.modifier] || 0);
118
+ return key.transpose(transposeDistance).normalize().useModifier(key.modifier);
119
+ }
120
+ }, {
121
+ key: "toChordSymbolString",
122
+ value: function toChordSymbolString(key) {
123
+ return this.toChordSymbol(key).toString();
124
+ }
103
125
  }, {
104
126
  key: "is",
105
127
  value: function is(type) {
@@ -127,21 +149,6 @@ var Key = /*#__PURE__*/function () {
127
149
  modifier = _ref2.modifier;
128
150
  return this.note.equals(note) && this.modifier === modifier;
129
151
  }
130
- }, {
131
- key: "toChordSymbol",
132
- value: function toChordSymbol(key) {
133
- if (this.is(_constants.SYMBOL)) {
134
- return this.clone();
135
- }
136
-
137
- var transposeDistance = this.note.getTransposeDistance() + (MODIFIER_TRANSPOSITION[this.modifier] || 0);
138
- return key.transpose(transposeDistance).normalize().useModifier(key.modifier);
139
- }
140
- }, {
141
- key: "toChordSymbolString",
142
- value: function toChordSymbolString(key) {
143
- return this.toChordSymbol(key).toString();
144
- }
145
152
  }, {
146
153
  key: "toNumeric",
147
154
  value: function toNumeric(key) {
@@ -159,11 +166,11 @@ var Key = /*#__PURE__*/function () {
159
166
  note: 1
160
167
  });
161
168
  var symbolKey = key.clone();
162
- var reference = this.clone().normalize().useModifier(key.modifier);
169
+ var reference = this.clone().normalize().useModifier(key.modifier).normalizeEnharmonics(key);
163
170
 
164
171
  while (!symbolKey.equals(reference)) {
165
172
  numericKey = numericKey.transposeUp().useModifier(key.modifier);
166
- symbolKey = symbolKey.transposeUp().normalize().useModifier(key.modifier);
173
+ symbolKey = symbolKey.transposeUp().normalize().useModifier(key.modifier).normalizeEnharmonics(key);
167
174
  }
168
175
 
169
176
  return numericKey;
@@ -190,11 +197,11 @@ var Key = /*#__PURE__*/function () {
190
197
  note: 'I'
191
198
  });
192
199
  var symbolKey = key.clone();
193
- var reference = this.clone().normalize().useModifier(key.modifier);
200
+ var reference = this.clone().normalize().useModifier(key.modifier).normalizeEnharmonics(key);
194
201
 
195
202
  while (!symbolKey.equals(reference)) {
196
203
  numeralKey = numeralKey.transposeUp().useModifier(key.modifier);
197
- symbolKey = symbolKey.transposeUp().normalize().useModifier(key.modifier);
204
+ symbolKey = symbolKey.transposeUp().normalize().useModifier(key.modifier).normalizeEnharmonics(key);
198
205
  }
199
206
 
200
207
  return numeralKey;
@@ -318,6 +325,21 @@ var Key = /*#__PURE__*/function () {
318
325
  });
319
326
  }
320
327
 
328
+ return this.clone();
329
+ }
330
+ }, {
331
+ key: "normalizeEnharmonics",
332
+ value: function normalizeEnharmonics(key) {
333
+ if (key) {
334
+ var rootKeyString = key.minor ? "".concat(key, "m") : key.toString();
335
+ var enharmonics = _enharmonicNormalize["default"][rootKeyString];
336
+ var thisKeyString = this.toString();
337
+
338
+ if (enharmonics && enharmonics[thisKeyString]) {
339
+ return Key.parse(enharmonics[thisKeyString]);
340
+ }
341
+ }
342
+
321
343
  return this.clone();
322
344
  }
323
345
  }], [{
@@ -355,7 +377,8 @@ var Key = /*#__PURE__*/function () {
355
377
  function _set2(attributes) {
356
378
  return new this.constructor(_objectSpread({
357
379
  note: this.note.clone(),
358
- modifier: this.modifier
380
+ modifier: this.modifier,
381
+ minor: !!this.minor
359
382
  }, attributes));
360
383
  }
361
384
 
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _default = {
8
+ 'Ab': {
9
+ 'B': 'Cb'
10
+ },
11
+ 'Cb': {
12
+ 'B': 'Cb',
13
+ 'A#': 'Bb',
14
+ 'E': 'Fb'
15
+ },
16
+ 'C': {
17
+ 'C#': 'Db',
18
+ 'D#': 'Eb',
19
+ 'F#': 'Gb',
20
+ 'G#': 'Ab',
21
+ 'A#': 'Bb'
22
+ },
23
+ 'C#': {
24
+ 'D#': 'Eb',
25
+ 'A#': 'Bb'
26
+ },
27
+ 'Db': {
28
+ 'B': 'Cb'
29
+ },
30
+ 'D': {
31
+ 'D#': 'Eb',
32
+ 'A#': 'Bb'
33
+ },
34
+ 'E': {
35
+ 'Ab': 'G#',
36
+ 'A#': 'Bb',
37
+ 'D#': 'Eb'
38
+ },
39
+ 'F': {
40
+ 'A#': 'Bb',
41
+ 'F#': 'Gb',
42
+ 'C#': 'Db',
43
+ 'D#': 'Eb',
44
+ 'G#': 'Ab'
45
+ },
46
+ 'F#': {
47
+ 'A#': 'Bb',
48
+ 'D#': 'Eb'
49
+ },
50
+ 'Gb': {
51
+ 'A#': 'Bb',
52
+ 'D#': 'Eb',
53
+ 'G#': 'Ab',
54
+ 'B': 'Cb',
55
+ 'E': 'Fb'
56
+ },
57
+ 'G': {
58
+ 'A#': 'Bb',
59
+ 'D#': 'Eb',
60
+ 'G#': 'Ab'
61
+ },
62
+ 'G#': {
63
+ 'A#': 'Bb',
64
+ 'D#': 'Eb'
65
+ },
66
+ 'Am': {
67
+ 'Gb': 'G',
68
+ 'G#': 'Ab',
69
+ 'F#': 'Gb',
70
+ 'C#': 'Db',
71
+ 'D#': 'Eb',
72
+ 'A#': 'Bb'
73
+ },
74
+ 'Bbm': {
75
+ 'Cb': 'B'
76
+ },
77
+ 'Bm': {
78
+ 'A#': 'Bb',
79
+ 'D#': 'Eb'
80
+ },
81
+ 'C#m': {
82
+ 'A#': 'Bb',
83
+ 'D#': 'Eb'
84
+ },
85
+ 'Cm': {
86
+ 'G#': 'Ab',
87
+ 'A#': 'Bb',
88
+ 'D#': 'Eb',
89
+ 'F#': 'Gb',
90
+ 'C#': 'Db'
91
+ },
92
+ 'Dm': {
93
+ 'A#': 'Bb',
94
+ 'D#': 'Eb'
95
+ },
96
+ 'Em': {
97
+ 'A#': 'Bb',
98
+ 'D#': 'Eb'
99
+ },
100
+ 'F#m': {
101
+ 'A#': 'Bb',
102
+ 'D#': 'Eb',
103
+ 'Gb': 'F#',
104
+ 'Ab': 'G#',
105
+ 'Db': 'C#'
106
+ },
107
+ 'Fm': {
108
+ 'G#': 'Ab',
109
+ 'A#': 'Bb',
110
+ 'D#': 'Eb',
111
+ 'F#': 'Gb',
112
+ 'C#': 'Db'
113
+ },
114
+ 'Gm': {
115
+ 'G#': 'Ab',
116
+ 'A#': 'Bb',
117
+ 'D#': 'Eb'
118
+ },
119
+ 'G#m': {
120
+ 'A#': 'Bb',
121
+ 'D#': 'Eb'
122
+ }
123
+ };
124
+ exports["default"] = _default;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
7
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
8
+
9
+ var fs = require('fs');
10
+
11
+ console.warn('\x1b[34m', '👷 Building suffix normalize mapping from suffix-mapping.txt');
12
+ fs.readFile('src/normalize_mappings/suffix-mapping.txt', function (err, data) {
13
+ if (err) throw err;
14
+ var suffixs = [];
15
+ data.toString().split('\n').map(function (line) {
16
+ var items = line.split(',');
17
+ var cleanStringsArray = [];
18
+ items.forEach(function (item) {
19
+ cleanStringsArray.push(item.trim());
20
+ });
21
+ suffixs.push(cleanStringsArray);
22
+ return suffixs;
23
+ });
24
+ var flatObject = {};
25
+ suffixs.forEach(function (line) {
26
+ line.forEach(function (item) {
27
+ flatObject = _objectSpread(_defineProperty({}, item, line[0]), flatObject);
28
+ });
29
+ });
30
+ var suffixMappingJson = JSON.stringify(flatObject, null, 2);
31
+ var suffixMappingJs = "export default ".concat(suffixMappingJson, ";\n");
32
+ fs.writeFile('src/normalize_mappings/suffix-normalize-mapping.js', suffixMappingJs, 'utf-8', function (error) {
33
+ if (error) throw error;
34
+ console.warn('\x1b[32m', '✨ Sucessfully built suffix-normalize-mapping.js');
35
+ });
36
+ });