interface PolicyResult {
  [key: string]: string[];
}

export = function (policy: string): PolicyResult {
  return policy.split(';').reduce<PolicyResult>((result, directive) => {
    const trimmed = directive.trim();
    if (!trimmed) {
      return result;
    }

    const split = trimmed.split(/\s+/g);
    const key = split.shift() as string;

    if (!Object.prototype.hasOwnProperty.call(result, key)) {
      result[key] = split;
    }

    return result;
  }, {});
}

interface PolicyResult {
  [key: string]: string[];
}

export = (policy: string): PolicyResult =>
  policy.split(';').reduce<PolicyResult>((result, directive) => {
    const [directiveKey, ...directiveValue] = directive.trim().split(/\s+/g);

    if (!directiveKey || Object.prototype.hasOwnProperty.call(result, directiveKey)) {
      return result;
    } else {
      return {
        ...result,
        [directiveKey]: directiveValue,
      };
    }
  }, {})


Rewrite internals to reduce LOC and remove a type cast
Rewrite internals to reduce LOC and remove a type cast
