MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / format

Function format

python/pythonmonkey/builtin_modules/util.js:235–323  ·  view source on GitHub ↗
(f)

Source from the content-addressed store, hash-verified

233}
234
235function format(f)
236{
237 let i, tempStr;
238 if (typeof f !== 'string')
239 {
240 if (arguments.length === 0) return '';
241 let res = '';
242 for (i = 0; i < arguments.length - 1; i++)
243 {
244 res += inspect(arguments[i]);
245 res += ' ';
246 }
247 res += inspect(arguments[i]);
248 return res;
249 }
250
251 if (arguments.length === 1) return f;
252
253 let str = '';
254 let a = 1;
255 let lastPos = 0;
256 for (i = 0; i < f.length - 1; i++)
257 {
258 if (f.charCodeAt(i) === 37)
259 { // '%'
260 const nextChar = f.charCodeAt(++i);
261 if (a !== arguments.length)
262 {
263 switch (nextChar)
264 {
265 case 115: // 's'
266 tempStr = String(arguments[a++]);
267 break;
268 case 106: // 'j'
269 tempStr = tryStringify(arguments[a++]);
270 break;
271 case 100: // 'd'
272 tempStr = `${Number(arguments[a++])}`;
273 break;
274 case 79: // 'O'
275 tempStr = inspect(arguments[a++]);
276 break;
277 case 111: // 'o'
278 tempStr = inspect(arguments[a++],
279 { showHidden: true, depth: 4, showProxy: true });
280 break;
281 case 105: // 'i'
282 tempStr = `${parseInt(arguments[a++], 10)}`;
283 break;
284 case 102: // 'f'
285 tempStr = `${parseFloat(arguments[a++])}`;
286 break;
287 case 37: // '%'
288 str += f.slice(lastPos, i);
289 lastPos = i + 1;
290 continue;
291 default: // any other character is not a correct placeholder
292 continue;

Callers 2

constructorMethod · 0.85
#formatToStrMethod · 0.85

Calls 2

inspectFunction · 0.85
tryStringifyFunction · 0.85

Tested by

no test coverage detected