3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @author Jacky Nguyen <jacky@sencha.com>
17 * @docauthor Jacky Nguyen <jacky@sencha.com>
20 * A utility class that wrap around a string version number and provide convenient
21 * method to perform comparison. See also: {@link Ext.Version#compare compare}. Example:
23 var version = new Ext.Version('1.0.2beta');
24 console.log("Version is " + version); // Version is 1.0.2beta
26 console.log(version.getMajor()); // 1
27 console.log(version.getMinor()); // 0
28 console.log(version.getPatch()); // 2
29 console.log(version.getBuild()); // 0
30 console.log(version.getRelease()); // beta
32 console.log(version.isGreaterThan('1.0.1')); // True
33 console.log(version.isGreaterThan('1.0.2alpha')); // True
34 console.log(version.isGreaterThan('1.0.2RC')); // False
35 console.log(version.isGreaterThan('1.0.2')); // False
36 console.log(version.isLessThan('1.0.2')); // True
38 console.log(version.match(1.0)); // True
39 console.log(version.match('1.0.2')); // True
45 // Current core version
46 var version = '4.0.7', Version;
47 Ext.Version = Version = Ext.extend(Object, {
50 * @param {String/Number} version The version number in the follow standard format: major[.minor[.patch[.build[release]]]]
51 * Examples: 1.0 or 1.2.3beta or 1.2.3.4RC
52 * @return {Ext.Version} this
54 constructor: function(version) {
55 var parts, releaseStartIndex;
57 if (version instanceof Version) {
61 this.version = this.shortVersion = String(version).toLowerCase().replace(/_/g, '.').replace(/[\-+]/g, '');
63 releaseStartIndex = this.version.search(/([^\d\.])/);
65 if (releaseStartIndex !== -1) {
66 this.release = this.version.substr(releaseStartIndex, version.length);
67 this.shortVersion = this.version.substr(0, releaseStartIndex);
70 this.shortVersion = this.shortVersion.replace(/[^\d]/g, '');
72 parts = this.version.split('.');
74 this.major = parseInt(parts.shift() || 0, 10);
75 this.minor = parseInt(parts.shift() || 0, 10);
76 this.patch = parseInt(parts.shift() || 0, 10);
77 this.build = parseInt(parts.shift() || 0, 10);
83 * Override the native toString method
85 * @return {String} version
87 toString: function() {
92 * Override the native valueOf method
94 * @return {String} version
101 * Returns the major component value
102 * @return {Number} major
104 getMajor: function() {
105 return this.major || 0;
109 * Returns the minor component value
110 * @return {Number} minor
112 getMinor: function() {
113 return this.minor || 0;
117 * Returns the patch component value
118 * @return {Number} patch
120 getPatch: function() {
121 return this.patch || 0;
125 * Returns the build component value
126 * @return {Number} build
128 getBuild: function() {
129 return this.build || 0;
133 * Returns the release component value
134 * @return {Number} release
136 getRelease: function() {
137 return this.release || '';
141 * Returns whether this version if greater than the supplied argument
142 * @param {String/Number} target The version to compare with
143 * @return {Boolean} True if this version if greater than the target, false otherwise
145 isGreaterThan: function(target) {
146 return Version.compare(this.version, target) === 1;
150 * Returns whether this version if smaller than the supplied argument
151 * @param {String/Number} target The version to compare with
152 * @return {Boolean} True if this version if smaller than the target, false otherwise
154 isLessThan: function(target) {
155 return Version.compare(this.version, target) === -1;
159 * Returns whether this version equals to the supplied argument
160 * @param {String/Number} target The version to compare with
161 * @return {Boolean} True if this version equals to the target, false otherwise
163 equals: function(target) {
164 return Version.compare(this.version, target) === 0;
168 * Returns whether this version matches the supplied argument. Example:
170 * var version = new Ext.Version('1.0.2beta');
171 * console.log(version.match(1)); // True
172 * console.log(version.match(1.0)); // True
173 * console.log(version.match('1.0.2')); // True
174 * console.log(version.match('1.0.2RC')); // False
176 * @param {String/Number} target The version to compare with
177 * @return {Boolean} True if this version matches the target, false otherwise
179 match: function(target) {
180 target = String(target);
181 return this.version.substr(0, target.length) === target;
185 * Returns this format: [major, minor, patch, build, release]. Useful for comparison
188 toArray: function() {
189 return [this.getMajor(), this.getMinor(), this.getPatch(), this.getBuild(), this.getRelease()];
193 * Returns shortVersion version without dots and release
196 getShortVersion: function() {
197 return this.shortVersion;
216 * Converts a version component to a comparable value
219 * @param {Object} value The value to convert
222 getComponentValue: function(value) {
223 return !value ? 0 : (isNaN(value) ? this.releaseValueMap[value] || value : parseInt(value, 10));
227 * Compare 2 specified versions, starting from left to right. If a part contains special version strings,
228 * they are handled in the following order:
229 * 'dev' < 'alpha' = 'a' < 'beta' = 'b' < 'RC' = 'rc' < '#' < 'pl' = 'p' < 'anything else'
232 * @param {String} current The current version to compare to
233 * @param {String} target The target version to compare to
234 * @return {Number} Returns -1 if the current version is smaller than the target version, 1 if greater, and 0 if they're equivalent
236 compare: function(current, target) {
237 var currentValue, targetValue, i;
239 current = new Version(current).toArray();
240 target = new Version(target).toArray();
242 for (i = 0; i < Math.max(current.length, target.length); i++) {
243 currentValue = this.getComponentValue(current[i]);
244 targetValue = this.getComponentValue(target[i]);
246 if (currentValue < targetValue) {
248 } else if (currentValue > targetValue) {
266 lastRegisteredVersion: null,
269 * Set version number for the given package name.
271 * @param {String} packageName The package name, for example: 'core', 'touch', 'extjs'
272 * @param {String/Ext.Version} version The version, for example: '1.2.3alpha', '2.4.0-dev'
275 setVersion: function(packageName, version) {
276 Ext.versions[packageName] = new Version(version);
277 Ext.lastRegisteredVersion = Ext.versions[packageName];
283 * Get the version number of the supplied package name; will return the last registered version
284 * (last Ext.setVersion call) if there's no package name given.
286 * @param {String} packageName (Optional) The package name, for example: 'core', 'touch', 'extjs'
287 * @return {Ext.Version} The version
289 getVersion: function(packageName) {
290 if (packageName === undefined) {
291 return Ext.lastRegisteredVersion;
294 return Ext.versions[packageName];
298 * Create a closure for deprecated code.
300 // This means Ext.oldMethod is only supported in 4.0.0beta and older.
301 // If Ext.getVersion('extjs') returns a version that is later than '4.0.0beta', for example '4.0.0RC',
302 // the closure will not be invoked
303 Ext.deprecate('extjs', '4.0.0beta', function() {
304 Ext.oldMethod = Ext.newMethod;
309 * @param {String} packageName The package name
310 * @param {String} since The last version before it's deprecated
311 * @param {Function} closure The callback function to be executed with the specified version is less than the current version
312 * @param {Object} scope The execution scope (<tt>this</tt>) if the closure
315 deprecate: function(packageName, since, closure, scope) {
316 if (Version.compare(Ext.getVersion(packageName), since) < 1) {
320 }); // End Versioning
322 Ext.setVersion('core', version);