Skip to content

Commit 07fcef2

Browse files
committed
removed prefix interface
The prefix interface was not really helpfull and caused more problems then it solved. Removing it made a lot of sense. You can easily generate your prefixed units with the Grunt command, just add a new unit in ;
1 parent 5e168a9 commit 07fcef2

109 files changed

Lines changed: 499 additions & 591 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Gruntfile.js

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,84 @@ module.exports = function(grunt) {
55

66
// Quickly generate all SI prefixed units
77
grunt.registerTask('default', function() {
8-
var siPrefixes = 'src/Prefix/Metric/',
9-
unitFolder = 'src/Unit/',
8+
var siPrefixes = [
9+
{ factor: '-24', symbol: 'y', label: 'yocto' },
10+
{ factor: '-21', symbol: 'z', label: 'zepto' },
11+
{ factor: '-18', symbol: 'a', label: 'atto' },
12+
{ factor: '-15', symbol: 'f', label: 'femto' },
13+
{ factor: '-12', symbol: 'p', label: 'pico' },
14+
{ factor: '-9', symbol: 'n', label: 'nano' },
15+
{ factor: '-6', symbol: 'μ', label: 'micro' },
16+
{ factor: '-3', symbol: 'm', label: 'milli' },
17+
{ factor: '-2', symbol: 'c', label: 'centi' },
18+
{ factor: '-1', symbol: 'd', label: 'deci' },
19+
{ factor: '1', symbol: 'da', label: 'deca' },
20+
{ factor: '2', symbol: 'h', label: 'hecto' },
21+
{ factor: '3', symbol: 'k', label: 'kilo' },
22+
{ factor: '6', symbol: 'M', label: 'mega' },
23+
{ factor: '9', symbol: 'G', label: 'giga' },
24+
{ factor: '12', symbol: 'T', label: 'tera' },
25+
{ factor: '15', symbol: 'P', label: 'peta' },
26+
{ factor: '18', symbol: 'E', label: 'exa' },
27+
{ factor: '21', symbol: 'Z', label: 'zetta' },
28+
{ factor: '24', symbol: 'Y', label: 'yotta' },
29+
];
30+
var unitFolder = 'src/Unit/',
1031
fileExt = '.php',
1132
baseUnits = {},
12-
units = { 'Mass' : '',
13-
'Length': '',
14-
'Area': 'Square',
15-
'Volume': 'Cubic',
16-
};
17-
phpTemplate = `<?php
18-
namespace PhpMeasurements\\Unit\\<%= unit %>;
33+
units = [
34+
{ unit: 'Mass', symbol: 'g', label: 'gram', prefix: '' },
35+
{ unit: 'Length', symbol: 'm', label: 'meter', prefix: '' },
36+
{ unit: 'Area', symbol: 'm', label: 'meter', prefix: 'Square' },
37+
{ unit: 'Volume', symbol: 'm', label: 'meter', prefix: 'Cubic' },
38+
];
39+
phpTemplate = `<?php
40+
namespace PhpUnitConversion\\Unit\\<%= unit %>;
1941
20-
use PhpMeasurements\\System\\Metric;
21-
use PhpMeasurements\\Prefix\\Metric\\<%= prefixSI %>;
42+
use PhpUnitConversion\\System\\Metric;
2243
23-
class <%= baseUnitPrefix %><%= prefixSI %><%= baseUnit %> extends <%= baseUnitPrefix %><%= baseUnit %> implements Metric, <%= prefixSI %>
44+
class <%= baseUnitPrefix %><%= prefixSI %><%= baseUnit %> extends <%= baseUnitPrefix %><%= baseUnit %> implements Metric
2445
{
25-
46+
const FACTOR = <%= factor %>;
47+
48+
const SYMBOL = '<%= symbol %>';
49+
const LABEL = '<%= label %>';
2650
}
2751
`;
2852
var baseRegex = /BASE_UNIT\s*=\s*([^:]+)::/,
2953
replaceExt = new RegExp(fileExt);
3054

31-
grunt.file.recurse(siPrefixes, function(abspath, rootdir, subdir, filename) {
32-
for(var unit in units) {
33-
var baseUnitPrefix = units[unit];
34-
35-
if(!baseUnits[unit]) {
36-
var baseContent = grunt.file.read(unitFolder + unit + fileExt),
37-
matches = baseRegex.exec(baseContent),
38-
baseUnit = matches[1].split('\\').pop();
39-
40-
baseUnit = baseUnit.replace(new RegExp(baseUnitPrefix), '');
41-
baseUnits[unit] = baseUnit;
42-
}else{
43-
baseUnit = baseUnits[unit];
44-
}
45-
46-
if(baseUnit) {
47-
var prefixSI = filename.replace(replaceExt, ''),
48-
phpFileName = unitFolder + unit + '/' + baseUnitPrefix + prefixSI + baseUnit + fileExt;
49-
50-
51-
if(!grunt.file.exists(phpFileName)) {
52-
grunt.file.write(phpFileName,
53-
grunt.template.process(phpTemplate, { data: { unit: unit, prefixSI: prefixSI, baseUnit: baseUnit, baseUnitPrefix: baseUnitPrefix }})
54-
);
55-
}
56-
}
55+
//grunt.file.recurse(siPrefixes, function(abspath, rootdir, subdir, filename) {
56+
for (var u = 0; u < units.length; u++) {
57+
var baseUnitPrefix = units[u].prefix,
58+
baseUnit = units[u].label.replace(/\b./g, function(m){ return m.toUpperCase(); })
59+
60+
if(baseUnit) {
61+
for (var i = 0; i < siPrefixes.length; i++) {
62+
siPrefix = siPrefixes[i]
63+
var prefixSI = siPrefix.label.replace(/\b./g, function(m){ return m.toUpperCase(); }) /*filename.replace(replaceExt, '')*/,
64+
phpFileName = unitFolder + units[u].unit + '/' + baseUnitPrefix + prefixSI + baseUnit + fileExt,
65+
factor = '1E' + siPrefix.factor * (baseUnitPrefix === 'Cubic' ? 3 : (baseUnitPrefix === 'Square' ? 2 : 1)),
66+
symbol = siPrefix.symbol + units[u].symbol + (baseUnitPrefix === 'Cubic' ? '3' : (baseUnitPrefix === 'Square' ? '2' : '')),
67+
label = (baseUnitPrefix ? baseUnitPrefix.toLowerCase() + ' ' : '') + siPrefix.label + units[u].label;
68+
69+
if(true||!grunt.file.exists(phpFileName)) {
70+
console.log(phpFileName);
71+
grunt.file.write(phpFileName,
72+
grunt.template.process(phpTemplate, { data: {
73+
unit: units[u].unit,
74+
factor: factor,
75+
symbol: symbol,
76+
label: label,
77+
prefixSI: prefixSI,
78+
baseUnit: baseUnit,
79+
baseUnitPrefix: baseUnitPrefix
80+
}})
81+
);
82+
}
5783
}
58-
});
59-
60-
61-
62-
84+
}
85+
}
86+
//});
6387
});
6488
};

src/Prefix.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Prefix/Metric.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Prefix/Metric/Atto.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Prefix/Metric/Centi.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Prefix/Metric/Deca.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Prefix/Metric/Deci.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Prefix/Metric/Exa.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Prefix/Metric/Femto.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Prefix/Metric/Giga.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)