source: imaps-frontend/node_modules/core-js/modules/esnext.string.dedent.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.0 KB
Line 
1'use strict';
2var FREEZING = require('../internals/freezing');
3var $ = require('../internals/export');
4var makeBuiltIn = require('../internals/make-built-in');
5var uncurryThis = require('../internals/function-uncurry-this');
6var apply = require('../internals/function-apply');
7var anObject = require('../internals/an-object');
8var toObject = require('../internals/to-object');
9var isCallable = require('../internals/is-callable');
10var lengthOfArrayLike = require('../internals/length-of-array-like');
11var defineProperty = require('../internals/object-define-property').f;
12var createArrayFromList = require('../internals/array-slice');
13var WeakMapHelpers = require('../internals/weak-map-helpers');
14var cooked = require('../internals/string-cooked');
15var parse = require('../internals/string-parse');
16var whitespaces = require('../internals/whitespaces');
17
18var DedentMap = new WeakMapHelpers.WeakMap();
19var weakMapGet = WeakMapHelpers.get;
20var weakMapHas = WeakMapHelpers.has;
21var weakMapSet = WeakMapHelpers.set;
22
23var $Array = Array;
24var $TypeError = TypeError;
25// eslint-disable-next-line es/no-object-freeze -- safe
26var freeze = Object.freeze || Object;
27// eslint-disable-next-line es/no-object-isfrozen -- safe
28var isFrozen = Object.isFrozen;
29var min = Math.min;
30var charAt = uncurryThis(''.charAt);
31var stringSlice = uncurryThis(''.slice);
32var split = uncurryThis(''.split);
33var exec = uncurryThis(/./.exec);
34
35var NEW_LINE = /([\n\u2028\u2029]|\r\n?)/g;
36var LEADING_WHITESPACE = RegExp('^[' + whitespaces + ']*');
37var NON_WHITESPACE = RegExp('[^' + whitespaces + ']');
38var INVALID_TAG = 'Invalid tag';
39var INVALID_OPENING_LINE = 'Invalid opening line';
40var INVALID_CLOSING_LINE = 'Invalid closing line';
41
42var dedentTemplateStringsArray = function (template) {
43 var rawInput = template.raw;
44 // https://github.com/tc39/proposal-string-dedent/issues/75
45 if (FREEZING && !isFrozen(rawInput)) throw new $TypeError('Raw template should be frozen');
46 if (weakMapHas(DedentMap, rawInput)) return weakMapGet(DedentMap, rawInput);
47 var raw = dedentStringsArray(rawInput);
48 var cookedArr = cookStrings(raw);
49 defineProperty(cookedArr, 'raw', {
50 value: freeze(raw)
51 });
52 freeze(cookedArr);
53 weakMapSet(DedentMap, rawInput, cookedArr);
54 return cookedArr;
55};
56
57var dedentStringsArray = function (template) {
58 var t = toObject(template);
59 var length = lengthOfArrayLike(t);
60 var blocks = $Array(length);
61 var dedented = $Array(length);
62 var i = 0;
63 var lines, common, quasi, k;
64
65 if (!length) throw new $TypeError(INVALID_TAG);
66
67 for (; i < length; i++) {
68 var element = t[i];
69 if (typeof element == 'string') blocks[i] = split(element, NEW_LINE);
70 else throw new $TypeError(INVALID_TAG);
71 }
72
73 for (i = 0; i < length; i++) {
74 var lastSplit = i + 1 === length;
75 lines = blocks[i];
76 if (i === 0) {
77 if (lines.length === 1 || lines[0].length > 0) {
78 throw new $TypeError(INVALID_OPENING_LINE);
79 }
80 lines[1] = '';
81 }
82 if (lastSplit) {
83 if (lines.length === 1 || exec(NON_WHITESPACE, lines[lines.length - 1])) {
84 throw new $TypeError(INVALID_CLOSING_LINE);
85 }
86 lines[lines.length - 2] = '';
87 lines[lines.length - 1] = '';
88 }
89 // eslint-disable-next-line sonarjs/no-redundant-assignments -- false positive, https://github.com/SonarSource/SonarJS/issues/4767
90 for (var j = 2; j < lines.length; j += 2) {
91 var text = lines[j];
92 var lineContainsTemplateExpression = j + 1 === lines.length && !lastSplit;
93 var leading = exec(LEADING_WHITESPACE, text)[0];
94 if (!lineContainsTemplateExpression && leading.length === text.length) {
95 lines[j] = '';
96 continue;
97 }
98 common = commonLeadingIndentation(leading, common);
99 }
100 }
101
102 var count = common ? common.length : 0;
103
104 for (i = 0; i < length; i++) {
105 lines = blocks[i];
106 quasi = lines[0];
107 k = 1;
108 for (; k < lines.length; k += 2) {
109 quasi += lines[k] + stringSlice(lines[k + 1], count);
110 }
111 dedented[i] = quasi;
112 }
113
114 return dedented;
115};
116
117var commonLeadingIndentation = function (a, b) {
118 if (b === undefined || a === b) return a;
119 var i = 0;
120 for (var len = min(a.length, b.length); i < len; i++) {
121 if (charAt(a, i) !== charAt(b, i)) break;
122 }
123 return stringSlice(a, 0, i);
124};
125
126var cookStrings = function (raw) {
127 var i = 0;
128 var length = raw.length;
129 var result = $Array(length);
130 for (; i < length; i++) {
131 result[i] = parse(raw[i]);
132 } return result;
133};
134
135var makeDedentTag = function (tag) {
136 return makeBuiltIn(function (template /* , ...substitutions */) {
137 var args = createArrayFromList(arguments);
138 args[0] = dedentTemplateStringsArray(anObject(template));
139 return apply(tag, this, args);
140 }, '');
141};
142
143var cookedDedentTag = makeDedentTag(cooked);
144
145// `String.dedent` method
146// https://github.com/tc39/proposal-string-dedent
147$({ target: 'String', stat: true, forced: true }, {
148 dedent: function dedent(templateOrFn /* , ...substitutions */) {
149 anObject(templateOrFn);
150 if (isCallable(templateOrFn)) return makeDedentTag(templateOrFn);
151 return apply(cookedDedentTag, this, arguments);
152 }
153});
Note: See TracBrowser for help on using the repository browser.