MCPcopy Create free account
hub / github.com/api-platform/website / doHumanization

Function doHumanization

pwa/utils/con/humanize-duration.js:120–241  ·  view source on GitHub ↗
(ms, options)

Source from the content-addressed store, hash-verified

118
119 // doHumanization does the bulk of the work.
120 function doHumanization(ms, options) {
121 var i, len, piece;
122
123 // Make sure we have a positive number.
124 // Has the nice sideffect of turning Number objects into primitives.
125 ms = Math.abs(ms);
126
127 var dictionary = getDictionary(options);
128 var pieces = [];
129
130 // Start at the top and keep removing units, bit by bit.
131 var unitName, unitMS, unitCount;
132 for (i = 0, len = options.units.length; i < len; i++) {
133 unitName = options.units[i];
134 unitMS = options.unitMeasures[unitName];
135
136 // What's the number of full units we can fit?
137 if (i + 1 === len) {
138 if (has(options, "maxDecimalPoints")) {
139 // We need to use this expValue to avoid rounding functionality of toFixed call
140 var expValue = Math.pow(10, options.maxDecimalPoints);
141 var unitCountFloat = ms / unitMS;
142 unitCount = parseFloat(
143 (Math.floor(expValue * unitCountFloat) / expValue).toFixed(
144 options.maxDecimalPoints
145 )
146 );
147 } else {
148 unitCount = ms / unitMS;
149 }
150 } else {
151 unitCount = Math.floor(ms / unitMS);
152 }
153
154 // Add the string.
155 pieces.push({
156 unitCount: unitCount,
157 unitName: unitName,
158 });
159
160 // Remove what we just figured out.
161 ms -= unitCount * unitMS;
162 }
163
164 var firstOccupiedUnitIndex = 0;
165 for (i = 0; i < pieces.length; i++) {
166 if (pieces[i].unitCount) {
167 firstOccupiedUnitIndex = i;
168 break;
169 }
170 }
171
172 if (options.round) {
173 var ratioToLargerUnit, previousPiece;
174 for (i = pieces.length - 1; i >= 0; i--) {
175 piece = pieces[i];
176 piece.unitCount = Math.round(piece.unitCount);
177

Callers 1

resultFunction · 0.85

Calls 3

hasFunction · 0.85
renderFunction · 0.85
getDictionaryFunction · 0.70

Tested by

no test coverage detected