@lowdefy/build
Version:
50 lines (46 loc) • 1.89 kB
JavaScript
/*
Copyright 2020-2026 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import { type } from '@lowdefy/helpers';
// Keys that look like operators (single key starting with _) but are not.
const KNOWN_NON_OPERATORS = new Set([
'_id'
]);
function walkAndCount(value, counter, parentConfigKey) {
if (type.isArray(value)) {
value.forEach((item)=>walkAndCount(item, counter, parentConfigKey));
return;
}
if (!type.isObject(value)) {
return;
}
const configKey = value['~k'] || parentConfigKey;
const keys = Object.keys(value);
// Check if this object is an operator (single key starting with _, ignoring ~ prefixed keys)
// This allows ~ignoreBuildCheck to be on the same object as an operator
const nonTildeKeys = keys.filter((k)=>!k.startsWith('~'));
if (nonTildeKeys.length === 1) {
const key = nonTildeKeys[0];
const [op] = key.split('.');
const operator = op.replace(/^(_+)/gm, '_');
if (operator.length > 1 && operator[0] === '_' && !KNOWN_NON_OPERATORS.has(operator)) {
counter.increment(operator, configKey);
}
}
// Recurse into all values
keys.forEach((key)=>{
walkAndCount(value[key], counter, configKey);
});
}
function countOperators(obj, { counter }) {
walkAndCount(obj, counter, null);
}
export default countOperators;