lodash#drop TypeScript Examples
The following examples show how to use
lodash#drop.
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: subWikiPlugin.ts From TidGi-Desktop with Mozilla Public License 2.0 | 6 votes |
/**
* Get "Sub-Wiki Plugin"'s content
* @param mainWikiPath subwiki's main wiki's absolute path.
* @returns ISubWikiPluginContent
*/
export async function getSubWikiPluginContent(mainWikiPath: string): Promise<ISubWikiPluginContent[]> {
if (mainWikiPath.length === 0) return [];
const FileSystemPathsTiddlerPath = getFileSystemPathsTiddlerPath(mainWikiPath);
try {
const FileSystemPathsFile = await fs.readFile(FileSystemPathsTiddlerPath, 'utf-8');
const FileSystemPaths = compact(drop(FileSystemPathsFile.split('\n'), 3));
return FileSystemPaths.map((line) => ({
tagName: getTagNameFromMatchPart(line),
folderName: getFolderNamePathPart(line),
})).filter((item) => item.folderName.length > 0 && item.tagName.length > 0);
} catch (error) {
logger.error((error as Error)?.message, { function: 'getSubWikiPluginContent' });
return [];
}
}
Example #2
Source File: index.ts From TidGi-Desktop with Mozilla Public License 2.0 | 5 votes |
/**
* Insert provided sub menu items into menubar, so user and services can register custom menu items
* @param menuID Top level menu name to insert menu items
* @param newSubMenuItems An array of menu item to insert or update, if some of item is already existed, it will be updated instead of inserted
* @param afterSubMenu The `id` or `role` of a submenu you want your submenu insert after. `null` means inserted as first submenu item; `undefined` means inserted as last submenu item;
* @param withSeparator Need to insert a separator first, before insert menu items
* @param menuPartKey When you update a part of menu, you can overwrite old menu part with same key
*/
public async insertMenu(
menuID: string,
newSubMenuItems: Array<DeferredMenuItemConstructorOptions | MenuItemConstructorOptions>,
afterSubMenu?: string | null,
withSeparator = false,
menuPartKey?: string,
): Promise<void> {
let foundMenuName = false;
const copyOfNewSubMenuItems = [...newSubMenuItems];
// try insert menu into an existed menu's submenu
for (const menu of this.menuTemplate) {
// match top level menu
if (menu.id === menuID) {
foundMenuName = true;
// heck some menu item existed, we update them and pop them out
const currentSubMenu = compact(menu.submenu);
// we push old and new content into this array, and assign back to menu.submenu later
let filteredSubMenu: Array<DeferredMenuItemConstructorOptions | MenuItemConstructorOptions> = currentSubMenu;
// refresh menu part by delete previous menuItems that belongs to the same partKey
if (menuPartKey !== undefined) {
filteredSubMenu = filteredSubMenu.filter((currentSubMenuItem) => !this.belongsToPart(menuPartKey, menuID, currentSubMenuItem.id));
}
for (const newSubMenuItem of newSubMenuItems) {
const existedItemIndex = currentSubMenu.findIndex((existedItem) => MenuService.isMenuItemEqual(existedItem, newSubMenuItem));
// replace existed item, and remove it from needed-to-add-items
if (existedItemIndex !== -1) {
filteredSubMenu[existedItemIndex] = newSubMenuItem;
remove(newSubMenuItems, (item) => item.id === newSubMenuItem.id);
}
}
if (afterSubMenu === undefined) {
// inserted as last submenu item
if (withSeparator) {
filteredSubMenu.push({ type: 'separator' });
}
filteredSubMenu = [...filteredSubMenu, ...newSubMenuItems];
} else if (afterSubMenu === null) {
// inserted as first submenu item
if (withSeparator) {
newSubMenuItems.push({ type: 'separator' });
}
filteredSubMenu = [...newSubMenuItems, ...filteredSubMenu];
} else if (typeof afterSubMenu === 'string') {
// insert after afterSubMenu
const afterSubMenuIndex = filteredSubMenu.findIndex((item) => item.id === afterSubMenu || item.role === afterSubMenu);
if (afterSubMenuIndex === -1) {
throw new InsertMenuAfterSubMenuIndexError(afterSubMenu, menuID, menu);
}
filteredSubMenu = [...take(filteredSubMenu, afterSubMenuIndex + 1), ...newSubMenuItems, ...drop(filteredSubMenu, afterSubMenuIndex - 1)];
}
menu.submenu = filteredSubMenu;
// leave this finding menu loop
break;
}
}
// if user wants to create a new menu in menubar
if (!foundMenuName) {
this.menuTemplate.push({
label: menuID,
submenu: newSubMenuItems,
});
}
// update menuPartRecord
if (menuPartKey !== undefined) {
this.updateMenuPartRecord(menuPartKey, menuID, copyOfNewSubMenuItems);
}
await this.buildMenu();
}
Example #3
Source File: subWikiPlugin.ts From TidGi-Desktop with Mozilla Public License 2.0 | 5 votes |
/**
* update $:/config/FileSystemPaths programmatically to make private tiddlers goto the sub-wiki
* @param {string} mainWikiPath main wiki's location path
* @param {Object} newConfig { "tagName": Tag to indicate that a tiddler belongs to a sub-wiki, "subWikiFolderName": folder name inside the subwiki/ folder }
* @param {Object} oldConfig if you need to replace a line, you need to pass-in what old line looks like, so here we can find and replace it
*/
export function updateSubWikiPluginContent(
mainWikiPath: string,
newConfig?: Pick<IWorkspace, 'tagName' | 'subWikiFolderName'>,
oldConfig?: Pick<IWorkspace, 'tagName' | 'subWikiFolderName'>,
): void {
const FileSystemPathsTiddlerPath = getFileSystemPathsTiddlerPath(mainWikiPath);
const FileSystemPathsFile = fs.readFileSync(FileSystemPathsTiddlerPath, 'utf-8');
let newFileSystemPathsFile = '';
// ignore the tags, title and type, 3 lines, and an empty line
const header = take(FileSystemPathsFile.split('\n'), 3);
const FileSystemPaths = compact(drop(FileSystemPathsFile.split('\n'), 3));
// if newConfig is undefined, but oldConfig is provided, we delete the old config
if (newConfig === undefined) {
if (oldConfig === undefined) {
throw new Error('Both newConfig and oldConfig are not provided in the updateSubWikiPluginContent() for\n' + JSON.stringify(mainWikiPath));
}
const { tagName, subWikiFolderName } = oldConfig;
if (typeof tagName !== 'string' || subWikiFolderName === undefined) {
throw new Error('tagName or subWikiFolderName is not string for in the updateSubWikiPluginContent() for\n' + JSON.stringify(mainWikiPath));
}
// find the old line, delete it
const newFileSystemPaths = FileSystemPaths.filter((line) => !(line.includes(getMatchPart(tagName)) && line.includes(getPathPart(subWikiFolderName))));
newFileSystemPathsFile = `${header.join('\n')}\n\n${newFileSystemPaths.join('\n')}`;
} else {
// if this config already exists, just return
const { tagName, subWikiFolderName } = newConfig;
if (typeof tagName !== 'string' || subWikiFolderName === undefined) {
throw new Error('tagName or subWikiFolderName is not string for in the updateSubWikiPluginContent() for\n' + JSON.stringify(mainWikiPath));
}
if (FileSystemPaths.some((line) => line.includes(getMatchPart(tagName)) && line.includes(getPathPart(subWikiFolderName)))) {
return;
}
// prepare new line
const newConfigLine = '[' + getMatchPart(tagName) + getPathPart(subWikiFolderName);
// if we are just to add a new config, just append it to the end of the file
const oldConfigTagName = oldConfig?.tagName;
if (oldConfig !== undefined && typeof oldConfigTagName === 'string') {
// find the old line, replace it with the new line
const newFileSystemPaths = FileSystemPaths.map((line) => {
if (line.includes(getMatchPart(oldConfigTagName)) && line.includes(getPathPart(oldConfig.subWikiFolderName))) {
return newConfigLine;
}
return line;
});
newFileSystemPathsFile = `${header.join('\n')}\n\n${newFileSystemPaths.join('\n')}`;
} else {
newFileSystemPathsFile = `${FileSystemPathsFile}\n${newConfigLine}`;
}
}
fs.writeFileSync(FileSystemPathsTiddlerPath, newFileSystemPathsFile);
}