@yuki-no/plugin-sdk
Version:
A GitHub Action that tracks changes between repositories. It creates GitHub issues based on commits from a head repository, making it ideal for documentation translation projects.
60 lines (59 loc) • 1.79 kB
JavaScript
import os from 'node:os';
import path from 'node:path';
export const assert = (condition, message) => {
if (!condition) {
throw new Error(message);
}
};
export const excludeFrom = (excludeSource, reference) => excludeSource.filter(sourceEl => !reference.includes(sourceEl));
export const chunk = (data, chunkSize) => {
if (chunkSize >= data.length) {
return [data];
}
if (chunkSize < 1) {
throw new Error('Invalid chunkSize');
}
return [...Array(Math.ceil(data.length / chunkSize))].map((_, ind) => data.slice(ind * chunkSize, (ind + 1) * chunkSize));
};
export const uniqueWith = (value, mapper) => {
if (value.length <= 1) {
return [...value];
}
const result = [];
const seen = new Set();
for (const v of value) {
const mapped = mapper(v);
if (seen.has(mapped)) {
continue;
}
result.push(v);
seen.add(mapped);
}
return [...result];
};
export const mergeArray = (a, b) => {
if (a.length === 0 && b.length === 0) {
return [];
}
if (a.length === 0) {
return [...b];
}
if (b.length === 0) {
return [...a];
}
return [...a, ...b];
};
export const isNotEmpty = (value) => {
const isNotNullable = value !== undefined && value !== null;
if (typeof value === 'string') {
return value.length > 0;
}
return isNotNullable;
};
export const unique = (value) => Array.from(new Set(value));
const COMMIT_URL_REGEX = /https:\/\/github\.com\/[^/]+\/[^/]+\/commit\/([a-f0-9]{7,40})/;
export const extractHashFromIssue = (issue) => {
const match = issue.body?.match(COMMIT_URL_REGEX);
return match?.[1];
};
export const createTempFilePath = (prefix) => path.join(os.tmpdir(), prefix);