<commit_before>import {IProposal, IProposalProvider, IRequest} from '../../provider-api';
import {includes, isString, trim, startsWith, flatten} from 'lodash';
import {search, versions} from './npm-service';

const DEPENDENCY_PROPERTIES = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies'];
const STABLE_VERSION_REGEX = /^(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)$/;
const EMPTY_OBJECT = {};

function createPackageNameProposal(key: string, request: IRequest): IProposal {
  const {isBetweenQuotes, shouldAddComma} = request;
  const proposal: IProposal = {}
  proposal.displayText = key;
  proposal.rightLabel = 'dependency';
  proposal.type = 'property';
  if (isBetweenQuotes) {
    proposal.text = key;
  } else {
    proposal.snippet = '"' + key + '": "$1"' + (shouldAddComma ? ',' : '');
  }
  return proposal;
}

function getUsedKeys(request: IRequest) {
  const {contents} = request;
  const safeContents = contents || EMPTY_OBJECT;
  return flatten(DEPENDENCY_PROPERTIES
    .map(property => safeContents[property] || EMPTY_OBJECT)
    .map(object => Object.keys(object)));
}

function createVersionProposal(version: string, request: IRequest): IProposal {
  const {isBetweenQuotes, shouldAddComma, token} = request;
  const proposal: IProposal = {}
  proposal.displayText = version;
  proposal.rightLabel = 'version';
  proposal.type = 'value';
  proposal.replacementPrefix = trim(token, '"');
  if (isBetweenQuotes) {
    proposal.text = version;
  } else {
    proposal.snippet = '"' + version + '"' + (shouldAddComma ? ',' : '');
  }
  return proposal;
}

function isStableVersion(version: string): boolean {
  return STABLE_VERSION_REGEX.test(version);
}

export default class PackageJsonDependencyProposalProvider implements IProposalProvider {
  getProposals(request: IRequest): Promise<Array<IProposal>> {
    const {segments, isKeyPosition, isValuePosition} = request;

    if (segments && segments.length === 1 && isKeyPosition) {
      const [key] = segments;
      if (includes(DEPENDENCY_PROPERTIES, key)) {
        return this.getDependencyKeysProposals(request);
      }<commit_msg>Added icons to package json dependecy proposals<commit_after>import {IProposal, IProposalProvider, IRequest} from '../../provider-api';
import {includes, isString, trim, startsWith, flatten} from 'lodash';
import {search, versions} from './npm-service';

const DEPENDENCY_PROPERTIES = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies'];
const STABLE_VERSION_REGEX = /^(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)$/;
const EMPTY_OBJECT = {};

function createPackageNameProposal(key: string, request: IRequest): IProposal {
  const {isBetweenQuotes, shouldAddComma} = request;
  const proposal: IProposal = {}
  proposal.displayText = key;
  proposal.rightLabel = 'dependency';
  proposal.type = 'property';
  if (isBetweenQuotes) {
    proposal.text = key;
  } else {
    proposal.snippet = '"' + key + '": "$1"' + (shouldAddComma ? ',' : '');
  }
  proposal.iconHTML = '<i class="icon-package"></i>';
  return proposal;
}

function getUsedKeys(request: IRequest) {
  const {contents} = request;
  const safeContents = contents || EMPTY_OBJECT;
  return flatten(DEPENDENCY_PROPERTIES
    .map(property => safeContents[property] || EMPTY_OBJECT)
    .map(object => Object.keys(object)));
}

function createVersionProposal(version: string, request: IRequest): IProposal {
  const {isBetweenQuotes, shouldAddComma, token} = request;
  const proposal: IProposal = {}
  proposal.displayText = version;
  proposal.rightLabel = 'version';
  proposal.type = 'value';
  proposal.replacementPrefix = trim(token, '"');
  if (isBetweenQuotes) {
    proposal.text = version;
  } else {
    proposal.snippet = '"' + version + '"' + (shouldAddComma ? ',' : '');
  }
  proposal.iconHTML = '<i class="icon-version"></i>';
  return proposal;
}

function isStableVersion(version: string): boolean {
  return STABLE_VERSION_REGEX.test(version);
}

export default class PackageJsonDependencyProposalProvider implements IProposalProvider {
  getProposals(request: IRequest): Promise<Array<IProposal>> {
    const {segments, isKeyPosition, isValuePosition} = request;

    if (segments && segments.length === 1 && isKeyPosition) {
      const [key] = segments;
      if (includes(DEPENDENCY_PROPERTIES, key)) {
        return this.getDependencyKeysProposals(request);
      }
{'fraction_non_alphanumeric': 0.15090090090090091, 'fraction_numerical': 0.0076576576576576575, 'mean_word_length': 4.882119205298014, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '142734', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1421, 'n_words': 296}