lodash#repeat TypeScript Examples

The following examples show how to use lodash#repeat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: compoundJSONSchemaYAML.ts    From hub with Apache License 2.0 6 votes vote down vote up
renderValueLine = (item: ItemValue, name: string): string => {
  let activeLevel = item.level;
  if (item.isArrayParent && (isUndefined(item.hasDecorator) || !item.hasDecorator)) {
    activeLevel = activeLevel + 1;
  }
  return `\n${activeLevel === 0 ? '\n' : ''}${
    item.title ? `${repeat(' ', activeLevel * 2)}# ${item.title}\n` : ''
  }${repeat(' ', activeLevel * 2)}${item.hasDecorator ? '- ' : ''}${name}: ${item.value || ''}`;
}
Example #2
Source File: compoundJSONSchemaYAML.ts    From hub with Apache License 2.0 4 votes vote down vote up
compoundJSONSchemaYAML = (schema: JSONSchema, savedOpts: { [key: string]: number }): FormattedValuesSchema => {
  const title = schema.title ? `# ${schema.title}` : '';
  let paths: string[] = [];

  const getValue = (value: JSONSchema, level: number): string | undefined => {
    if (isUndefined(value.default)) {
      return undefined;
    }

    if (isNull(value.default)) {
      return 'null';
    }
    switch (isArray(value.type) ? value.type[0] : value.type) {
      case 'object':
        return isEmpty(value.default) ? '{}' : JSON.stringify(value.default);
      case 'array':
        return isArray(value.default)
          ? value.default.length === 0
            ? `[]`
            : `${(value.default as string[]).map((val: string) => `\n${repeat(' ', (level + 1) * 2)}- ${val}`)}`
          : '';

      case 'boolean':
      case 'integer':
        return value.default!.toString();
      case 'string':
        const isLongText = (value.default as string).length > 40;
        if (isLongText) {
          return `|-\n\n${repeat(' ', (level + 1) * 2)}${value.default}`;
        } else {
          return formatStringForYAML(value.default as string) || `""`;
        }
      default:
        return value.default ? value.default.toString() : undefined;
    }
  };

  const items = {};

  function checkProperties(props: any, level: number, isArrayParent: boolean, pathSteps: string[] = [], path?: string) {
    Object.keys(props).forEach((propName: string, index: number) => {
      let value: JSONSchema | undefined = props[propName] as JSONSchema;
      let isCurrentArrayParent = false;
      if (
        value &&
        !isNull(value.type) &&
        value.type === 'array' &&
        value.items &&
        (value.items as JSONSchema).hasOwnProperty('properties')
      ) {
        isCurrentArrayParent = true;
      }

      const currentSteps: string[] = [...pathSteps, propName];
      const currentPath = getJMESPathForValuesSchema(isCurrentArrayParent ? `${propName}[0]` : propName, path);
      paths.push(currentPath);

      const checkCombinations = (valueToCheck: JSONSchema) => {
        if (valueToCheck.oneOf) {
          value = valueToCheck.oneOf[savedOpts[currentPath] || 0] as JSONSchema;
        } else if (valueToCheck.anyOf) {
          value = valueToCheck.anyOf[savedOpts[currentPath] || 0] as JSONSchema;
        }
      };

      if (value && isUndefined(value.$ref)) {
        checkCombinations(value);
      } else {
        value = undefined;
      }

      if (isUndefined(value) || isNull(value)) return;

      const defaultValue = getValue(value, level);

      if (isCurrentArrayParent) {
        set(items, currentSteps, {
          level: level,
          title: value.title,
          properties: {},
        });
        checkProperties(
          (value.items as JSONSchema).properties!,
          level + 1,
          isCurrentArrayParent,
          [...currentSteps, 'properties'],
          currentPath
        );
      } else {
        if (value.properties) {
          set(items, currentSteps, {
            level: level,
            title: value.title,
            properties: {},
            hasDecorator: isArrayParent && index === 0,
            isArrayParent: isArrayParent,
          });
          checkProperties(
            value.properties,
            isArrayParent ? level + 2 : level + 1,
            isCurrentArrayParent,
            [...currentSteps, 'properties'],
            currentPath
          );
        } else if (!isUndefined(defaultValue)) {
          set(items, currentSteps, {
            level: level,
            value: defaultValue,
            title: value.title,
            hasDecorator: isArrayParent && index === 0,
            isArrayParent: isArrayParent,
          });
        }
      }
    });
  }

  if (schema.properties) {
    checkProperties(schema.properties, 0, false);
  }

  const yamlContent = prepareContent(items);

  return {
    yamlContent: trim(yamlContent) !== '' ? `${title}${yamlContent}` : undefined,
    paths: paths,
  };
}