Skip to content

Commit ac5166b

Browse files
committed
Tests, error throwing, closure
1 parent 071e3c2 commit ac5166b

File tree

5 files changed

+188
-76
lines changed

5 files changed

+188
-76
lines changed

Link.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ $.Link (part of noUiSlider) - WTFPL */
55
/*jslint sub: true */
66
/*jslint white: true */
77

8-
// ==ClosureCompiler==
9-
// @externs_url http://refreshless.com/externs/jquery-1.8.js
10-
// @compilation_level ADVANCED_OPTIMIZATIONS
11-
// @warning_level VERBOSE
12-
// ==/ClosureCompiler==
13-
148
(function( $ ){
159

1610
'use strict';
1711

1812
// Throw an error if formatting options are incompatible.
1913
function throwEqualError( F, a, b ) {
2014
if ( (F[a] || F[b]) && (F[a] === F[b]) ) {
21-
throwError("(Link) '"+a+"' can't match '"+b+"'.'");
15+
throw new Error("(Link) '"+a+"' can't match '"+b+"'.'");
2216
}
2317
}
2418

@@ -65,7 +59,7 @@ var
6559
}
6660

6761
if ( typeof options !== 'object' ){
68-
throwError("(Format) 'format' option must be an object.");
62+
throw new Error("(Format) 'format' option must be an object.");
6963
}
7064

7165
var settings = {};
@@ -84,15 +78,15 @@ var
8478
// More can't be guaranteed due to floating point issues.
8579
if ( val === 'decimals' ){
8680
if ( options[val] < 0 || options[val] > 7 ){
87-
throwError("(Format) 'format.decimals' option must be between 0 and 7.");
81+
throw new Error("(Format) 'format.decimals' option must be between 0 and 7.");
8882
}
8983
}
9084

9185
settings[val] = options[val];
9286

9387
// If the value isn't valid, emit an error.
9488
} else {
95-
throwError("(Format) 'format."+val+"' must be a " + typeof FormatDefaults[i] + ".");
89+
throw new Error("(Format) 'format."+val+"' must be a " + typeof FormatDefaults[i] + ".");
9690
}
9791
});
9892

@@ -118,23 +112,24 @@ var
118112

119113
number = this.v('encoder')( number );
120114

121-
var negative = '', preNegative = '', base = '', mark = '';
115+
var decimals = this.v('decimals'),
116+
negative = '', preNegative = '', base = '', mark = '';
117+
118+
// Rounding away decimals might cause a value of -0
119+
// when using very small ranges. Remove those cases.
120+
if ( parseFloat(number.toFixed(decimals)) === 0 ) {
121+
number = '0';
122+
}
122123

123124
if ( number < 0 ) {
124125
negative = this.v('negative');
125126
preNegative = this.v('negativeBefore');
126127
}
127128

128129
// Round to proper decimal count
129-
number = Math.abs(number).toFixed( this.v('decimals') ).toString();
130+
number = Math.abs(number).toFixed(decimals).toString();
130131
number = number.split('.');
131132

132-
// Rounding away decimals might cause a value of -0
133-
// when using very small ranges. Remove those cases.
134-
if ( parseFloat(number) === 0 ) {
135-
number[0] = '0';
136-
}
137-
138133
// Group numbers in sets of three.
139134
if ( this.v('thousand') ) {
140135
base = reverse(number[0]).match(/.{1,3}/g);
@@ -167,10 +162,12 @@ var
167162
// The set request might want to ignore this handle.
168163
// Test for 'undefined' too, as a two-handle slider
169164
// can still be set with an integer.
170-
if( input === null || input === undefined ) {
165+
if ( input === null || input === undefined ) {
171166
return false;
172167
}
173168

169+
input = this.v('from')(input);
170+
174171
// Remove formatting and set period for float parsing.
175172
input = input.toString();
176173

@@ -189,7 +186,7 @@ var
189186
input = input.replace(new RegExp('^'+esc( this.v('prefix') )), '');
190187

191188
// Only replace if a negative sign is set.
192-
if ( this.v['negative'] ) {
189+
if ( this.v('negative') ) {
193190

194191
// Reset isNeg to prevent double '-' insertion.
195192
isNeg = '';
@@ -215,7 +212,7 @@ var
215212
return false;
216213
}
217214

218-
return this.v('from')(input);
215+
return input;
219216
};
220217

221218

@@ -273,6 +270,7 @@ var
273270

274271

275272
// Initialisor
273+
/** @constructor */
276274
Link.prototype.init = function ( target, method, format, update ) {
277275

278276
// Write all formatting to this object.
@@ -291,7 +289,7 @@ var
291289

292290
// If the string doesn't begin with '-', which is reserved, add a new hidden input.
293291
if ( typeof target === 'string' && target.indexOf('-') !== 0 ) {
294-
this.setHidden( target, method );
292+
this.setHidden( target );
295293
return;
296294
}
297295

@@ -327,7 +325,7 @@ var
327325

328326
// Nothing matched, throw error.
329327
throw new RangeError("(Link) Invalid Link.");
330-
}
328+
};
331329

332330
// Provides external items with the slider value.
333331
Link.prototype.write = function ( value, handle, slider, update ) {
@@ -358,16 +356,19 @@ var
358356

359357
// Set formatting options.
360358
Link.prototype.setFormatting = function ( options ) {
361-
this.formatting = new Format( $.extend({}, options, this.formatting ));
359+
this.formatting = new Format($.extend({},
360+
options,
361+
this.formatting instanceof Format ? this.formatting.settings : this.formatting
362+
));
362363
};
363364

364365
Link.prototype.setObject = function ( obj ) {
365366
this.obj = obj;
366-
}
367+
};
367368

368369
Link.prototype.setIndex = function ( index ) {
369370
this.N = index;
370-
}
371+
};
371372

372373
// Parses slider value to user defined display.
373374
Link.prototype.format = function ( a ) {

0 commit comments

Comments
 (0)